0

In Javascript you can use the spread syntax in a function call like this:

console.log(...[1,2,3]);

Is there an equivalent in Reason? I tried the following:

let bound = (number, lower, upper) => {
  max(lower, min(upper, number));
};

let parameters = (1,0,20);

bound(...parameters) |> Js.log;

But this gives an unknown syntax error:

Try reason snippet

Kasper
  • 12,594
  • 12
  • 41
  • 63

1 Answers1

1

There's not. Reason is a statically typed language, and lists are dynamically-sized and homogenous. It would be of very limited use, and not at all obvious how it would deal with too few or too many arguments. If you want to pass a list, you should just accept a list and deal with it appropriately, as a separate function if desired.

You could of course use a tuple instead, which is fixed-size and heterogenous, but I don't see a use-case for that either, since you might as well just call the function directly then.

For JavaScript FFI there is however the bs.splice attribute, which will allow you to apply a variable number of arguments to a js function using an array. But it needs to be called with an array literal, not just any array.

glennsl
  • 28,186
  • 12
  • 57
  • 75