我们可以打包模块以进行估值,然后将其拆包回模块(作为头等公民的模块)。另外,我们可以将模块类型打包为类型,但是...是否可以从类型中解压缩模块类型?如果是-怎么样?如果不是-为什么?以下草图显示了我的意思。
module type S = sig
type t
end
type 'a t = (module S with type t = 'a)
module type X = sig
include TO_SIG ( int t )
end
类型定义
type 'a t = (module S with type t = 'a)
实际上不是类型为S
的模块的[[packing,但类型为t
,但实际上是类型别名],该别名为类型表达式'a t
赋予了一个较短的名称(module S with type t = 'a)
,这表示类型的模块S
在它们定义的类型t
上是多态的。
'a t
且已知等于'a t
的任何地方,只要您具有(module S with type t = 'a)
类型的值,您都可以解压缩该值,甚至将其用作函子的参数。您甚至可以使用module type of
构造恢复打包模块的模块类型,例如,>let apply : type a. a t -> unit = fun (module S) ->
let module X = struct
module type S' = sig
include module type of S
end
end in ()
与类型相同