Reason ML 中的整数类型和模块是什么?

What are the integer types and modules in Reason ML?

我试图使用 Int32 库编写一些代码,但我遇到了类型错误:

let x : int = 7;
Int32.abs(x)

This has type:
  int
But somewhere wanted:
  int32

我对此感到有点惊讶,因为在其他语言中 int 只是 int32 的别名。

我的问题是:

这里比较了可用的各种整数数据类型、它们的类型、相关模块和文字语法:

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 位的,除非你需要 [=13= 提供的精确算术语义,否则你会使用这种类型]、int64nativeintIn32, Int64, and 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中很常见,语义略有不同:

  • int32int64 表现相同
  • int 表现得像 int32
  • nativeint 大多数情况下被视为 float,但有少数例外更 integer-like.

来源:https://bucklescript.github.io/docs/en/difference-from-native-ocaml.html#integers