0

我目前正在尝试 Reason 并面临一个我不明白的错误

这是我的代码:

let mult = (x:float, y:float):float => x * y;

当我用 BuckleScript 编译它时,我收到以下错误:

  We've found a bug for you!
  D:\1\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).

我不明白为什么编译器需要把 afloat变成一个inthere

4

2 回答 2

6

我的建议是删除类型并让推理处理它,这真的很好。

但是如果你想保留你的特定类型,那么你需要改变你的运营商。你所拥有的是将两个整数相乘。如果您想将两个浮点数相乘,请将其更改为:

let mult = (x:float, y:float):float => x *. y;

请注意,运算符从*to*.指示浮点数学。

在此处查看更多文档:https ://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#1_Floatingpointarithmetic

于 2018-01-09T20:25:44.107 回答
2

我不知道原因,但 * 运算符不是像 ocaml 中那样保留为整数吗?试试 *. 改为运算符

于 2018-01-10T02:42:01.943 回答