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 tend to always do records-of-functions because its easier to pass around, but it might because i don’t know something about rescript.

see the two examples:

module Example1 = {
  module type Printer = {
    let println: string => unit
  }
  
  module ConsolePrinter: Printer = {
    let println = Console.log
  }
  
  
  module SomeModule = (P: Printer) => {
    let doSomething = () => {
      P.println("hello")
    }
  }
  
  let main = () => {
    module SM = SomeModule(ConsolePrinter)
    SM.doSomething()
  }
}
module Example2 = {
  module Printer = {
    type t = {
      println: string => unit
    }
  }
  
  module ConsolePrinter = {
    let make = (): Printer.t => {
      { println: Console.log }
    }
  }
  
  module SomeModule = {
    type t = {
      printer: Printer.t
    }
    
    let make = (~printer) => {
      { printer: printer }
    }
    
    let doSomething = (sm) => {
      sm.printer.println("hello")
    }
  }
  
  let main = () => {
    let sm = SomeModule.make(~printer=ConsolePrinter.make())
    sm->SomeModule.doSomething
  }
}

when do i want to use one or the other? is there a preferred convention in rescript? can we pass modules around as first-class objects easily in e.g. a record or type. how?

get back with your thoughts


Viewing all articles
Browse latest Browse all 2592

Trending Articles