gpt4 book ai didi

types - reason-ml这是类型推断问题吗?

转载 作者:行者123 更新时间:2023-12-02 10:49:09 26 4
gpt4 key购买 nike

我目前正在尝试原因并遇到我不理解的错误

这是我的代码:

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

最佳答案

我的建议是删除类型并让其进行推理,这真的很好。

但是,如果要保留特定类型,则需要更改您的运算符。您拥有的是两个整数相乘。如果要乘两个浮点数,则将其更改为:

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

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

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

关于types - reason-ml这是类型推断问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48173459/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com