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

Does Rescript Still support impl ? (Or, any feature like trait constraints)

$
0
0

Rescript’s type system is a bit different, though usually you can get some of the same effects with Functors.

module type Base = {
  type t
}

module type Trait1 = (Base: Base) => {
  let function1: Base.t => unit
}

module type Trait2 = (Base: Base) => {
  let function2: Base.t => unit
}

module AllTogether = (Base: Base, Impl1: Trait1, Impl2: Trait2) => {
  module M1 = Impl1(Base)
  module M2 = Impl2(Base)
  let callFunctions = t => {
    M1.function1(t)
    M2.function2(t)
  }
}

module MyModule = {
  type t = ()
}

module Impl1 = (Base: Base) => {
  let function1 = _ => Console.log("Method 1 called")
}

module Impl2 = (Base: Base) => {
  let function2 = _ => Console.log("Method 2 called")
}

module JointImpl = AllTogether(MyModule, Impl1, Impl2)

let value: MyModule.t = ()
JointImpl.callFunctions(value)

Now, this is a bit…much, and extending it out into a real use case would probably hit a wall quickly, but we’re talking about a toy example, so feel free to give more impl code you’d like to see Rescriptified. :wink:


Viewing all articles
Browse latest Browse all 2592

Trending Articles