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

标签 integer ocaml reason reasonml

我试图使用 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 位的,除非您需要 提供的确切算术语义,否则您会使用这种类型int32int64nativeintIn32 , 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 中很常见,语义略有不同:

  • int32int64 行为相同
  • 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/

相关文章:

python - 使用整数时遇到 'NoneType' 类型错误

java - 从整数中解析数字

ocaml - 是否可以使用模块动态创建多态变体类型?

javascript - 在javascript中使用按位或转换为整数

检查两个整数数组是否具有相同的元素,无论它们的顺序如何

ocaml - 尝试通过 Lwt 教程,但似乎存在库问题

functional-programming - OCaml 中 let rec 的限制

ubuntu - 无法使用 OPAM 在 Ubuntu 14.04 上为 OCaml 安装核心和 utop

ocaml - OCaml 或 Reason 的调用图生成器

javascript - 将记录列表导出到不带标签的 JavaScript 对象数组