具有多个参数的 Reasonml 类型,错误异常 Failure("nth")
Reasonml type with multiple arguments, Error exception Failure("nth")
编译以下代码时出现错误
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.
当我将 Rectangle
更改为元组 (int, int) 时,它起作用了
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;
数据构造函数不能有多个参数吗?
谢谢
问题已提交给 buckelscript https://github.com/BuckleScript/bucklescript/issues/1822
两种变体都是完全有效的原因代码。你可以有多个参数的构造函数,你做对了。显然,问题出在 Js.log
函数中,这是一个神奇的函数,对于 n 元构造函数,魔法失败了。
所以,我的建议是 (i) 在 bucklescript 错误跟踪器中提交问题,并且 (ii) 不要使用神奇的 Js.log
函数,而是派生或编写自己的打印机函数并使用它.
编译以下代码时出现错误
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.
当我将 Rectangle
更改为元组 (int, int) 时,它起作用了
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;
数据构造函数不能有多个参数吗?
谢谢
问题已提交给 buckelscript https://github.com/BuckleScript/bucklescript/issues/1822
两种变体都是完全有效的原因代码。你可以有多个参数的构造函数,你做对了。显然,问题出在 Js.log
函数中,这是一个神奇的函数,对于 n 元构造函数,魔法失败了。
所以,我的建议是 (i) 在 bucklescript 错误跟踪器中提交问题,并且 (ii) 不要使用神奇的 Js.log
函数,而是派生或编写自己的打印机函数并使用它.