Binding function that takes (float, float) to allow it to be called with a tuple param without introducing intermediate function

Hello fellow rescript-ers!

I’ve written some bindings for the node-canvas library for fun, and while I was doing this I added in some helper functions to allow calling various functions with a tuple coordinate instead of named x and y params. This is working, but I am wondering if it is possible to create the tuple “aliases” without the compiler producing any extra functions. I feel someone may know a trick I am missing.

I would appreciate if someone could take a peek at this playground snippet and comment whether it is possible to avoid the lineTo2 function in the output by binding in a different way (while still allowing the convenience of calling with a tuple for x and y). See playground for more details!

Sadly no. There are varadic function bindings, but those aren’t limited to 2.

1 Like

:sob: Oh well, thanks for looking, I’ll see if using @variadic will do the trick!

So, abusing @variadic almost did the trick! This worked as desired only if I inlined the coordinate as an array, like [10., 10.], but when I pulled the array out of the function call (let p = [10., 10.] and then used p with lineTo) the compiler decided to produce the output Caml_splice_call.spliceObjApply(ctx, "lineTo", [p]);

The variadic test:

Yes, for a lot of these binding tricks they only compile to clean JS if the value is defined in the function call to the external. For all other cases the value created might be used by other parts of the code so the compiler takes the safe route.