Reasonml type with multiple arguments, Failure exception ("nth")

I have an 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 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 in a data constructor?

thank

The issue has been posted to buckelscript https://github.com/BuckleScript/bucklescript/issues/1822

+3


source to share


1 answer


Both are absolutely correct reason code. You can have multi-argument constructors and you are doing it right. Apparently the problem lies with the function Js.log

, which is a kind of magic function and with n-ary constructors, the magic fails.



So my suggestion is (i) post the problem to the bucklescript bug tracker and (ii) not use a magic function Js.log

, but rather output or write your own printer function and use it.

+3


source







All Articles