Skip to content

Functors leads to code duplication #7021

Open
@hellos3b

Description

@hellos3b

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions