【问题标题】:Typed integer constant in OCamlOCaml 中的类型整数常量
【发布时间】:2014-04-21 16:46:47
【问题描述】:

假设你有一些这样的代码

type foo_t = int64 

let do_something_with_foo (f : foo_t) = (* left to your imagination *)

你想用一个常量“foo”来调用它,像这样:

do_something_with_foo 99

你如何让编译器/解释器相信你的常量 99 实际上是一个foo_t

【问题讨论】:

    标签: type-conversion ocaml type-inference


    【解决方案1】:

    使用do_something_with_foo 99L。 常量99 的类型为int99L 的类型为int64

    这与类型别名foo_t = int64 无关。只要foo_t 的定义是可见的(即没有被包含模块的签名掩盖),任何int64 类型的值也有foo_t 类型,反之亦然。

    【讨论】:

      【解决方案2】:

      L 字母很重要。

      # 99L;;
      - : int64 = 99L
      # 99l;;
      - : int32 = 99l
      # 99;;
      - : int = 99
      

      【讨论】:

        猜你喜欢
        • 2015-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-06
        • 2016-10-16
        • 1970-01-01
        相关资源
        最近更新 更多