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

How to bind to a function that returns a class

$
0
0

I don’t know what you need this for…

// Set up the condition in JS
%%raw(`
class Foo {
  bar() {
      console.log("bar called")
    }
 }

 function getFoo() {
    return Foo
  }
`)

module type Foo = {
  type t
}
module FooInterface = {
  external getFooClass: unit => module(Foo) = "getFoo"
  external make: module(Foo) => module(Foo) = "new" 
  @send external bar: module(Foo) => unit = "bar"
}

let foo = FooInterface.getFooClass()->FooInterface.make
foo->FooInterface.bar

A little operator priority problem here. Just use new as the name :sweat_smile:

To add constraints from the interface you can

module FooInterface = {
  type t
  external getFooClass: unit => module(Foo with type t = t) = "getFoo"
  external make: module(Foo with type t = t) => module(Foo with type t = t) = "new" 
  @send external bar: module(Foo with type t = t) => unit = "bar"
}

Viewing all articles
Browse latest Browse all 2592

Trending Articles