Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functors leads to code duplication #7021

Open
hellos3b opened this issue Sep 6, 2024 · 1 comment
Open

Functors leads to code duplication #7021

hellos3b opened this issue Sep 6, 2024 · 1 comment

Comments

@hellos3b
Copy link
Contributor

hellos3b commented Sep 6, 2024

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

@cometkim
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants