【发布时间】:2022-01-15 15:32:08
【问题描述】:
我正在使用 OCaml 中的 Map 模块。考虑以下代码来创建一个以整数为键的映射:
module Int = struct
type t = int
let compare a b = a - b
end
module IntMap = Map.Make(Int)
let m = IntMap.(empty |> add 3 "hello")
这一切都很好。它的编译和行为符合我的预期。
但是,如果我为 Int 模块添加类型注释,那么顶行变为:
module Int : Map.OrderedType = struct
最后一行导致编译出错:
let m = IntMap.(empty |> add 3 "hello")
^
Error: This expression has type int but an expression was expected of type
IntMap.key = Int.t
但是IntMap.key 和Int.t 都只是int 的别名。此外,Int 模块的类型为Map.OrderedType。我知道这一点,因为这是 Map.Make 所需的类型。
那么地球在这里发生了什么?为什么提供不必要的类型注释会导致这样的错误。类型注释是否会导致更严格的可访问性并且与推断类型的行为不同?
【问题讨论】:
标签: dictionary module ocaml standard-library