I have got Error while compiling the following code
type shape =
| Circle int
| Square int
| Rectangle int int;
let myShape = Circle 10;
let area =
switch myShape {
| Circle r => float_of_int (r * r) *. 3.14
| Square w => float_of_int (w * w)
| Rectangle w h => float_of_int (w * h)
};
Js.log area;
Fatal error: exception Failure("nth")
ninja: build stopped: subcommand failed.
When I change the Rectangle
to tuple (int, int), it works
type shape =
| Circle int
| Square int
| Rectangle (int, int);
let myShape = Circle 10;
let area =
switch myShape {
| Circle r => float_of_int (r * r) *. 3.14
| Square w => float_of_int (w * w)
| Rectangle (w, h) => float_of_int (w * h)
};
Js.log area;
Is it not possible to have multiple arguments on a data constructor?
thanks
Issue has been submitted to buckelscript https://github.com/BuckleScript/bucklescript/issues/1822
Both variants are perfectly valid Reason code. You can have constructors with multiple arguments, and you're doing it right. Apparently, the problem is in the Js.log
function, that is sort of a magic function and with the n-ary constructors,the magic fails.
So, my suggestion is (i) to submit an issue in the bucklescript bug tracker and (ii) do not use the magic Js.log
function, but rather derive or write your own printer function and use it.