How do I bind the context to which the `call` refers in ReScript?

I had encounter following code which implemented by TypeScript:

class A {

  constructor(dispatch: (action) => void) {
    this.props.dispatch = dispatch;
  }

  dispatch(action) {
    this.props.dispatch.call(this, action)
  }
}

function dispatch(action) {
  // can use `this` and `action` here.
}

new A(dispatch)

And in ReScript, I binding it as:

// A.res
type t

@module('A') @new
let make = ((Action.t) => unit ) => t

When I try to create an instance of Module A, the question is coming: how can I use the closures in dispatch function:

let dispatch = action => {
  // Only can use action.
}

let a = A.make(dispatch);

I tired to use following syntax, but it had been throw Error:

let a = A.make((action) => {
  a.xxxxx // error
})

Must dispatch be outside the module or the class?

For me it’s a bit clearer if you use the module A { ... } syntax instead…

1 Like

You can use @this decorator

2 Likes