我试图使用 Int32
库编写一些代码,但我遇到了类型错误:
let x : int = 7;
Int32.abs(x)
This has type:
int
But somewhere wanted:
int32
对此我有点惊讶,因为在其他语言中,int
只是 int32
的别名。
我的问题是:
- Reason ML 中可用的整数类型有哪些?
- 使用它们的准则是什么?
- 每个都有一个模块吗? (例如,我注意到有
Int32.abs
但没有Int.abs
)
最佳答案
以下是可用的各种整数数据类型、它们的类型、相关模块和字面量语法的比较:
let int : int = Pervasives.abs(42);
let int32 : int32 = Int32.abs(42l);
let int64 : int64 = Int64.abs(42L);
let nativeint : nativeint = Nativeint.abs(42n);
请注意 Pervasives
是自动打开的,因此您不需要像上面那样限定其功能。
ìnt
在 32 位平台上是 31 位的,在 64 位平台上是 63 位的,除非您需要 提供的确切算术语义,否则您会使用这种类型int32
、int64
或 nativeint
。 In32
, Int64
, 和 Nativeint
都包含这个(或类似的)注释:
Performance notice: values of type int32 occupy more memory space than values of type int, and arithmetic operations on int32 are generally slower than those on int. Use int32 only when the application requires exact 32-bit arithmetic.
我希望能回答你的问题。
编辑:当使用 BuckleScript 编译成 JavaScript 时,这在 Reason 中很常见,语义略有不同:
int32
和int64
行为相同int
的行为类似于int32
nativeint
大多被视为float
,但有少数异常(exception)情况更像整数。
来源:https://bucklescript.github.io/docs/en/difference-from-native-ocaml.html#integers
关于integer - Reason ML 中的整数类型和模块是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48840436/