Bind using Rescript Record VS Getters and Setters

I ran into a problem when trying to bind a Javascript class instance to a Rescript Record. I managed to get it working with getters and setters no problem. In the documentation, the getters and setters are said to be an “alternative” to other binding options (with my preference being to bind to a record). I was wondering if there are specific use cases when you have to use them rather than binding to a record?

Extra Thought:
When diving into the compiled JS the problem seemed to be the Curry._1. If I, for example, tried to bind to this record:

type math = {
   add: (int,int)=>int
}

The output of using math.add(2,5) it would give me this.

var x = Curry._1(Math, add(2,5))

But it would fail. However, if I change the output to this:

var x = Math.add(2,5)

everything works. Are those two expressions not equivalent?

But of course, when using getters and setters to bind, Rescript compiles to the second version. I’d love to understand what’s happening in the background so that I don’t run into this issue again! :smiley:

I think @send is still recommended when bindings to methods for the reason you mention, but I think this can be circumvented with using uncurried functions:

type math = {
   add: (. int,int)=>int
}
... 
math.add(. 1, 2) 

Note the . before the input types or variables.

I guess you can potentially run into other issues like this being incorrect when using records with methods but I’ve never really understood in which cases!

2 Likes

Yeah the @send binding definitely looks better than the uncurried function. And using a pipe also helps. Still intrigued by the internals though! :smiley:

But I guess as a rule it’s safer to just use @send for methods then, thanks :slight_smile: