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

Difficulties with functors and abstract types

$
0
0

It is possible to get this to work for concrete types while retaining the module type signature. It relies on explicitly setting the type of FunctorType.a in MyMod’s type signature.

You would need to create a differently named version of MyMod separately for each type you want to support.

module type FunctorType = {
  type a
  let injectedRun: a => unit
}

module Functor = (R: FunctorType) => {
  let runInjectedRun = x => R.injectedRun(x)
}

// This is where you set an explicit type
// Specifying the a in FunctorType helps the compiler match things up
module MyMod: FunctorType with type a = string = {
  type a = string
  let injectedRun = (x: a) => Console.log(x)
}

module NewMod = Functor(MyMod)
NewMod.runInjectedRun("hello")

Playground Link


Viewing all articles
Browse latest Browse all 2592

Trending Articles