重复类型和模块类型定义可以避免将它们移动到外部.ml
文件。让我们来看看下面的例子:
module M : sig
(* m.mli *)
module type S = sig
type t
val x : t
end
module type Result = sig
type t
val xs : t list
end
module Make(A : S) : Result with type t = A.t
end = struct
(* m.ml *)
module type S = sig
type t
val x : t
end
module type Result = sig
type t
val xs : t list
end
module Make(A : S) = struct
type t = A.t
let xs = [A.x;A.x]
end
end
而不是写两个文件m.mli
和m.ml
的,我用了一个模块M
有一个明确的签名:这相当于有两个文件,您可以通过复制试穿OCaml的顶层 - 和粘贴。
在M
,东西被骗了sig .. end
和struct .. end
。如果模块类型变大,这很麻烦。
你可以通过将它们移动到另一个.ml
文件来共享这些软件。例如,像下面n_intf.ml
:
module N_intf = struct
(* n_intf.ml *)
module type S = sig
type t
val x : t
end
module type Result = sig
type t
val xs : t list
end
end
module N : sig
(* n.mli *)
open N_intf
module Make(A : S) : Result with type t = A.t
end = struct
(* n.ml *)
open N_intf
module Make(A : S) = struct
type t = A.t
let xs = [A.x;A.x]
end
end
您还可以使用*_intf.mli
代替*_intf.ml
,但我建议使用*_intf.ml
,因为:
- 模块包装不采取
mli
只有模块进去,因此你安装时必须复制*_intf.cmi
。
- 从类型定义(如ppx_deriving)生成代码需要在
.ml
中定义的东西。在这个例子中,没有这种情况,因为没有类型定义。
我最近尝试了第二个选项,但未能使它与ocamlbuild一起构建。你能发布一个用ocamlbuild构建的最小工作示例吗? – user3240588 2015-03-03 10:37:19
您能否提供第一个选项的示例?我如何从.mli ppx_import签名?第一个选项 – choeger 2015-03-03 11:00:34
,camlspotter回答。 – Drup 2015-03-03 14:31:21