【问题标题】:reason-ml is this a type inference issue?reason-ml 这是一个类型推断问题吗?
【发布时间】:2019-04-28 20:04:27
【问题描述】:

我目前正在尝试 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).

我不明白为什么编译器需要在这里将 float 转换为 int

【问题讨论】:

    标签: types compiler-errors reason


    【解决方案1】:

    我的建议是去掉类型,让推理来处理,真的很好。

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

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

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

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

    【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 2016-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 2012-10-16
      相关资源
      最近更新 更多