所以我在程序中使用 Jane Street 的 Core.std 来处理某些事情,但仍然想使用标准的 OCaml Map。但是,当我调用像
mem
这样的函数时,它需要 Core.std 版本的签名。怎样才能跨过这个坎呢?
open Core.Std
open Map
module PortTable = Map.Make(String)
let portTable = PortTable.empty
let string_add = (Int64.to_string packet.dlDst) in
PortTable.mem string_add portTable
这不会为我编译,因为它期待 Core.std 的
mem
版本,而不是标准版本:
错误:此表达式的类型为字符串,但表达式应为类型 'a PortTable.t = (string, 'a, PortTable.Key.comparator_witness) t
我只想使用标准的。这怎么办?
Core.Std
库通过Caml
模块公开标准库,因此,您只需在其名称前加上Caml.
前缀即可访问标准库中的任何值,例如,
module PortableMap = Caml.Map.Make(String)
这里有一个建议:
module StdMap = Map
open Core.Std
module PortTable = StdMap.Make(String)
以下是展示其工作原理的会议摘录:
# module PortTable = StdMap.Make(String);;
module PortTable :
sig
type key = Core.Std.String.t
type 'a t = 'a Map.Make(Core.Std.String).t
val empty : 'a t
val is_empty : 'a t -> bool
val mem : key -> 'a t -> bool
...
end
#
请注意,
PortTable
是从标准OCaml Map.Make函子创建的,但String
是来自Core的函子。您可以使用类似的技巧来保留标准 OCaml 字符串模块的名称。
(就我个人而言,我不会打开
StdMap
模块;命名空间已经相当拥挤了。)