[英]OCaml standard Map vs. Jane Street Core.std Map
因此,我在程序中使用Jane Street的Core.std進行某些操作,但仍想使用標准的OCaml地圖。 但是,當我調用諸如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版本,而不是標准版本:
Error: This expression has type string but an expression was expected of type
'a PortTable.t = (string, 'a, PortTable.Key.comparator_witness) t
我只想使用標准之一。 如果有人可以提供幫助,將不勝感激。
Core.Std
庫通過Caml
模塊公開了標准庫,因此,您只需在名稱庫中添加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
模塊;名稱空間已經很擁擠了。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.