Functors leads to code duplication
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
})
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.
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.