reason-ml 这是类型推断问题吗?
reason-ml is this a type inference issue?
我目前正在尝试 Reason 并遇到一个我不理解的错误
这是我的代码:
let mult = (x:float, y:float):float => x * y;
当我使用 BuckleScript 编译它时,出现以下错误:
We've found a bug for you!
D:\bbl\orga\src\demo.re 1:40
1 Ôöé let mult = (a:float, b:float):float => a * b;
This has type:
float
But somewhere wanted:
int
You can convert a float to a int with int_of_float.If this is a literal, you want a number without a trailing dot (e.g. 20).
我不明白为什么编译器需要在这里将 float
变成 int
我的建议是去掉类型,让推理来处理,真的很好。
但是如果你想保留你的特定类型,那么你需要改变你的运营商。你所拥有的是将两个整数相乘。如果你想乘以两个浮点数,你把它改成:
let mult = (x:float, y:float):float => x *. y;
注意运算符从 *
变为 *.
以指示浮点数学。
在此处查看更多文档:https://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#1_Floatingpointarithmetic
我不知道为什么,但 * 运算符不是像 ocaml 中那样保留给整数吗?试试*。运算符代替
我目前正在尝试 Reason 并遇到一个我不理解的错误
这是我的代码:
let mult = (x:float, y:float):float => x * y;
当我使用 BuckleScript 编译它时,出现以下错误:
We've found a bug for you!
D:\bbl\orga\src\demo.re 1:40
1 Ôöé let mult = (a:float, b:float):float => a * b;
This has type:
float
But somewhere wanted:
int
You can convert a float to a int with int_of_float.If this is a literal, you want a number without a trailing dot (e.g. 20).
我不明白为什么编译器需要在这里将 float
变成 int
我的建议是去掉类型,让推理来处理,真的很好。
但是如果你想保留你的特定类型,那么你需要改变你的运营商。你所拥有的是将两个整数相乘。如果你想乘以两个浮点数,你把它改成:
let mult = (x:float, y:float):float => x *. y;
注意运算符从 *
变为 *.
以指示浮点数学。
在此处查看更多文档:https://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#1_Floatingpointarithmetic
我不知道为什么,但 * 运算符不是像 ocaml 中那样保留给整数吗?试试*。运算符代替