如何将打包为“类型”的“模块类型”解压缩回“模块类型”?

问题描述 投票:0回答:1

我们可以打包模块以进行估值,然后将其拆包回模块(作为头等公民的模块)。另外,我们可以将模块类型打包为类型,但是...是否可以从类型中解压缩模块类型?如果是-怎么样?如果不是-为什么?以下草图显示了我的意思。

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 
ocaml first-class-modules
1个回答
0
投票

类型定义

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 ()

与类型相同
© www.soinside.com 2019 - 2024. All rights reserved.