【问题标题】:Ocaml: Exporting a type in an mli fileOcaml:在 mli 文件中导出类型
【发布时间】:2011-02-03 06:40:34
【问题描述】:

我有一个文件 context.ml,其中定义了地图

module CtxMap = Map.make(struct type t = int let compare = compare end)

还有一个函数map_get,类型为CtxMap.key -> 'a CtxMap.t -> 'a

如何将 CtxMap 声明添加到 context.mli 文件? 因为 mli 文件不能包含代码,所以我找不到办法。

【问题讨论】:

    标签: ocaml


    【解决方案1】:
    module CtxMap : Map.S with type key = int
    

    在 ocaml 提供的 map.ml 文件中,函子的签名名称是 S,而 key 是您要向外部模块公开的唯一抽象类型。

    【讨论】:

      【解决方案2】:

      作为参考,你总是可以这样做:

      ocamlc -i -c context.ml
      

      将默认的.mli 文件输出到标准输出。唯一的问题(在您的情况下)是它扩展了地图的签名。

      【讨论】:

      • 您需要添加 -c 以避免添加所有依赖项——此选项编译,但不链接。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多