rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Functors leads to code duplication

Open hellos3b opened this issue 1 year ago • 1 comments

While trying to use Functors for dependency injection, I noticed that the functions defined within module Make are defined a second time at the top level.

Rescript:

module type Dependency = {
  let sideEffect: string => promise<'a>
}

module Make = (D: Dependency) => {
  let exec = async () => {
    let anything = await D.sideEffect("bar")
    "HELLO !" ++ anything
  }
}

module Actual = Make({
  let sideEffect = %todo
})

let _ = Actual.exec()

Output:

Note that exec is defined both within Make(D) and at the top level.

function Make(D) {
  var exec = async function () {
    var anything = await D.sideEffect("bar");
    return "HELLO !" + anything;
  };
  return {
          exec: exec
        };
}

var sideEffect = Js_exn.raiseError("playground.res:13:250-255 - Todo");

async function exec() {
  var anything = await sideEffect("bar");
  return "HELLO !" + anything;
}

var Actual = {
  exec: exec
};

Expected:

I'd expect that the Actual implementation to use the module factory function:

var Actual = Make({
  sideEffect
})

Sample in Rescript Playground

hellos3b avatar Sep 06 '24 22:09 hellos3b

You're right. The current behavior of the functors is not ideal.

We need to provide more options to perform DCE and choose whether to inline or not.

cometkim avatar Sep 10 '24 20:09 cometkim

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Sep 06 '25 02:09 github-actions[bot]