2017-07-14 51 views
1

因此,我在我的程序中使用Jane Street的Core.std来处理某些事情,但仍想使用标准OCaml映射。但是,当我调用像mem这样的函数时,它期待着Core.std版本的签名。我如何克服这个障碍?谢谢!OCaml标准映射vs.简街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的,不是标准之一:

Error: This expression has type string but an expression was expected of type 
    'a PortTable.t = (string, 'a, PortTable.Key.comparator_witness) t 

我只是想使用标准之一。如果有人可以帮助,将不胜感激。

回答

4

Core.Std库通过Caml模块暴露标准库,因此,您可以从标准访问任何价值通过只是Caml.如前缀名称库,

module PortableMap = Caml.Map.Make(String) 
3

这里有一个建议:

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模块;命名空间已经相当拥挤。)

+0

本地打开可能是值得在这里过,而不是在文件打开全球的一个模块,尤其是当一个模块有一个拥挤的命名空间 –

+0

@NickZuber,核心库的设计在这样的洼地y,你应该打开Core.Std,因为它是覆盖标准库。 – ivg

+0

@ivg我在这里指的是Map模块 –