ocaml模块类型中的委托类型*非*抽象
我想用ocaml模块类型中的委托类型*非*抽象,ocaml,Ocaml,我想用类型t定义一个模块类型。因此,任何类型的实现者都必须选择类型t。是否有任何方法可以从模块type定义中保证该type t不是抽象的。假设我在我的mli文件中有这个定义: module type TYP = sig type t val f: t->unit end module M:TYP 使用这种模块类型,任何想调用M.f的人都会倒霉,因为类型t是抽象的,因此无法生成它。当然,我可以这样做 module type TYP = sig type t val f: t
类型t
定义一个模块类型。因此,任何类型
的实现者都必须选择类型t
。是否有任何方法可以从模块type
定义中保证该type t
不是抽象的。假设我在我的mli文件中有这个定义:
module type TYP = sig
type t
val f: t->unit
end
module M:TYP
使用这种模块类型,任何想调用M.f
的人都会倒霉,因为类型t
是抽象的,因此无法生成它。当然,我可以这样做
module type TYP = sig
type t
val f: t->unit
end
module M:TYP with type t=int
但是有没有办法“要求”t类型不抽象,这样任何实现者都必须公开它呢?这是不可能的。但您可以要求用户向您提供构造函数,如下所示:
module type T : sig
type t
val create : unit -> t
val f : t -> unit
end
我认为需求将来自于具有模块签名的模块的最终实用性。