顶层解释器的输出具有指导意义。
# module type I = sig
type t
end
module EQ (M : I) = struct
let equal (x : M.t) (y : M.t) = x = y
end
(* Explicitly type declaration *)
module A : I = struct
type t = string
end
module EStr1 = EQ (A);;
module type I = sig type t end
module EQ : functor (M : I) -> sig val equal : M.t -> M.t -> bool end
module A : I
module EStr1 : sig val equal : A.t -> A.t -> bool end
注意签名
EStr1
模块。
EStr1.equal
具有类型
A.t -> A.t -> bool
.不是类型
string -> string -> bool
.
应用
I
签名到
A
限制了我们了解
什么
A.t
是。它是一种抽象类型。你还没这么做
B
,所以
EStr2.equal
确实有类型
string->string->bool
.
您还可以显式公开此类型信息。
# module A : I with type t = string = struct
type t = string
end;;
module A : sig type t = string end
# module C = EQ (A);;
module C : sig val equal : string -> string -> bool end
一旦你理解了抽象类型的工作原理,它们就非常有用。