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!

https://rescript-lang.org/try?code=PTAEF5NAhBJA5AIgg4gZVAalAFwK4AOANgKYC0AhgMZUkE4CWAdgOagVEMUDOEkAUDgCeBEqCoUmANx4AlEkwAmJAE4BhAPZMcJAB44ATIv4ABbgsWg9OlUw6hOTEgBUNALlAAKCdLkXVmtrWRgA0oAB+uh4AZkQaFDhh4UIxcQkAlBAAfKB4TAw4EKAARI4uGsX8-CDsnDy4ABYJ7DR0OLz4xGJUGhoqiswJJB44DQy8AO59ANa8AEZ4hcw4KhqKeLS8klb6KhSg0XlUjFr8pIVlrgZF3ji6YQQenrHxiQdpOOmZ4DkA3vygBwkQqee6gITfUAEAHiO5kLKXDSeSJJCH8AC+VRqozEKhI3EKGmioAAtkJQHNmANWFs8aANCSCjpLNwNFC8TolI1xqAWAwpPiKdRpuxQDJOJYfDJuPIlAEtDp9EZTCS1nhSJ5itFesVMtZVHYiLzgYFFYZFE9vjkpX45eoFcFLOASiwTQ6lYpKtUwJAoABRAAaAEEALIABQAMn7QGoAPKIaO+8BVc6w3RFV04U2OzzpFPMcoGW5gzwARgADAA6MIVytfUDe0CwUATSSFHHsOYaAUOAu4NncBgk4jkkiMnAeY66SuIsvlmvlvON2BMUDcPAkkkUFRCMI8gpQjTcQdzUj9ilU8+IltjM8cOITZhsdOSSzknBsuZiAirKQMZSWPU+ydKQjavrUrLsFIGj-k+xpOHsjCsI0YjLKoJIkAMQy9k4VwHEcJxMAA-EAA
:nerd_face:

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:
https://rescript-lang.org/try?code=PTAEF5NAhBJA5AIgg4gZQpAUAFwJ4AOApqAMYCGAdgG7kDOASkZQCZEBOAwgPaU5EAPHACYWWLAAFa7AJbkWM0pLrMWoQf3aVyAG1A6ZlIgBVuALlAAKCjXpNWHHnw2iANKHLt25PAB4AZjrc5DgAfACUEKGgAK6UMjgQoABEBkamyVgaHNp6ONwAgl4+FpaWgcE47hUh4ZHg0Z7efjVhSckApDJsfAl4mVggoDgAFiTsRHSJ3P6gALZ4oABGhgqUAOZ0HhOg3HMJ-Gp03KAEE-yswyMyW+sy1JPL5KQA1h6gtAZqNrSMqo68fhCUSSObcFgxHRESzJfzcbjJSLZLS6UDrIg4JxAkQsUr1aI-Oz-LiAlxqcApdGY0nAlgDIaQKAAUQAGgUALIABQAMkzQJwAPKIPmM8DiKGJUg4ARJKlYsmWcJYKUCAC0oTSJm4lgA2gBGAAMADp3IajQBdSJDFACoWgAUAVWMnKdAEIsBLTkl9cbTcbzcrperNaZLAQrWB4ALjKBhWhYAwmYhQGYdYMwLBQAB3KiJUYkchLbgPfSGEj5UB0GRzAg6RZEfY4Cwqo0h7UEHUG83uDt6y1AA

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.