当你写一个程序使用两个文件amodule.ml
和bmodule.ml
,它们中的每一个都自动定义一个模块,名字叫Amodule
和Bmodule
,模块的内容就是你写到文件中的东西。
编译方法:
1 | ocamlopt -c amodule.ml |
为了避免使用;;
常这样写
1 | open Amodule;; |
接口(Interfaces)和签名(Signatures)
如amodule.ml:
1 | let message = "Hello" |
为隐藏message,可以编写amodule.mli文件
1 | val hello : unit -> unit |
==ocamldoc是什么?==
抽象类型Abstract Types 在mli文件中只给出类型名字 例如type date
子模块Submodules
1 | module xxx = struct |
子模块接口 模块类型 Module Types
1 | module xxx : sig |
函子 仿函数Functors 和类C语言的Functors不太一样
用另一个模块来参数化的模块 允许传入一个类型作为参数 大概是类似于C++的模板
整型的集合
1 | module Int_set = Set.Make (struct |
(可以看出 module只是起别名的作用 类似于let 重要的是struct-end对)
1 | module String_set = Set.Make (String);; |
带有一个参数的函子可以这样来定义:
1 | module F (X : X_type) = struct |
X_type是签名 此处是必须的
如果想要约束返回模块的签名
1 | module F (X : X_type) : Y_type = |
创建一个包含之前模块的模块用
include xxx