Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 2592

Module functors vs. records-of-functions for interface/implementation separation

$
0
0

I think passing records of functions is quite uncommon in ReScript. The more idiomatic convention would be to use modules or even just pass the function as a parameter:

module Example3 = {
 
  module SomeModule = {
    let doSomething = (~println) => {
      println("hello")
    }
  }
  
  let main = () => {
    SomeModule.doSomething(~println=Console.log)
  }
}

You can actually pass modules around using “first-class modules”, but I don’t think it’s documented today since the use cases are quite uncommon. Example:

module type Foo = {
  let print: unit => unit
}

let bar = (foo: module(Foo)) => {
  let module(Foo) = foo
  Foo.print()
}

module Foo = {
  let print = () => Console.log("hello")
}

bar(module(Foo))

Viewing all articles
Browse latest Browse all 2592

Trending Articles