函数在OCaml中的单独文件?
问题描述:
我想要一个很大的函子Hello(Blah:Blah_type)并将其保存在文件hello.ml中,但我该怎么做?函数在OCaml中的单独文件?
如果我只是在我的顶层文件,我不得不 模块你好(布拉赫:Blah_type)= 结构 VAL X = 2 结束
但我怎么把论点打招呼。毫升?我不能只让整个文件为“val x = 2”...?
答
这是不可能的。源文件始终表示为普通模块,而不是函子。这是轻松解决一个额外的开放。
答
OCamlPro有编译器补丁和外部工具,它可以支持这样的:
http://www.ocamlpro.com/blog/2011/08/10/ocaml-pack-functors.html
据我所知,官方的编译器版本不支持.ml文件作为函子。
答
,其内容与
module type S = sig
(* ... *)
end
module Hello (M : S) = struct
(* ... *)
end
module M : S = struct
(* ... *)
end
module H = Hello(M)
(* ... *)
与真正的代码示例补充ygrek的答案,而不是文件foo.ml
的你可以有hello.ml
与内容
module type S = sig
(* ... *)
end
module Make (M : S) = struct
(* ... *)
end
和foo.ml
改写为
module M : Hello.S = struct
(* ... *)
end
module H = Hello.Make(M)
(* ... *)
PS:如果你觉得很混淆,密码为M : S
或M : Hello.S
的模块是可选的(无论如何,M将被强制为这个签名),这只是为了说明如何做到这一点。
当想要在多个编译单元中分割函子时,bigfunctor补丁很有用。然而,这是过度的,在这里只是将一个仿函数作为单个编译单元的模块项包装起来。 – gasche 2012-04-12 11:13:57
我把这个问题解释为寻找一种方法来避免Make in Hello.Make(...)。补丁和ocp-pack可能是沉重的方法,但据我所知它们是目前唯一可用的。 – hcarty 2012-04-12 21:09:39