Module 如何包括工作?
我有 及 及 我想象编译后的测试是这样的Module 如何包括工作?,module,compiler-errors,include,ocaml,functor,Module,Compiler Errors,Include,Ocaml,Functor,我有 及 及 我想象编译后的测试是这样的 module Test = struct include Make (struct type t = ForWrap end) let f = function | Wrapped ForWrap -> () end 但实际上,它不是可编译的代码。OCaml告诉我: module Test = struct type t = ForWrap type d = Wrapped of t let f = fun
module Test = struct
include Make (struct type t = ForWrap end)
let f = function | Wrapped ForWrap -> ()
end
但实际上,它不是可编译的代码。OCaml告诉我:
module Test = struct
type t = ForWrap
type d = Wrapped of t
let f = function | Wrapped ForWrap -> ()
end
错误:取消绑定构造函数ForWrap
我不明白为什么。
我的解决方案有什么问题?让我们看看
Make(struct type t=ForWrapp end)
的签名:
ocamlc-c-i xxx.ml
显示此模块的签名:
module M = Make(struct type t = ForWrapp end)
请注意,结果模块中没有构造函数ForWrapp
。这就是代码不进行类型检查的原因
为什么构造函数不见了?这是因为函子
Make
的参数签名是T
T
定义了一个抽象的类型T
。即使您将Make
应用于具有更详细签名的模块(此处为struct type t=ForWrapp end
),它也会被强制降为t
,并且构造函数信息会丢失。functor应用程序是一个运行时操作,您不能将include
视为纯粹的语法级替换
module Test = struct
type t = ForWrap
type d = Wrapped of t
let f = function | Wrapped ForWrap -> ()
end
module Test = struct
include Make (struct type t = ForWrap end)
let f = function | Wrapped ForWrap -> ()
^^^^^^^
end
module M = Make(struct type t = ForWrapp end)
module M : sig
type t
type d = Wrrapped of t
end