如何包括工作?
How does include work?
我有
module type T = sig
type t
end
和
module Make (TypeProvider : T) = struct
include TypeProvider
type d = Wrapped of t
end
和
module Test = struct
include Make (struct type t = ForWrap end)
let f = function | Wrapped ForWrap -> ()
end
我想象编译后测试像
module Test = struct
type t = ForWrap
type d = Wrapped of t
let f = function | Wrapped ForWrap -> ()
end
但实际上,它不是可编译的代码。 OCaml 说我:
module Test = struct
include Make (struct type t = ForWrap end)
let f = function | Wrapped ForWrap -> ()
^^^^^^^
Error: Unbound constructor ForWrap
end
我不明白为什么。
我的解决方案有什么问题?
让我们看看Make (struct type t = ForWrapp end)
的签名:
module M = Make(struct type t = ForWrapp end)
ocamlc -c -i xxx.ml
显示此模块的签名:
module M : sig
type t
type d = Wrrapped of t
end
请注意,构造函数 ForWrapp
在结果模块中不可用。这就是您的代码不进行类型检查的原因。
为什么构造函数不见了?这是因为函子 Make
的参数签名是 T
。 T
定义了一个抽象类型 t
。即使您将 Make
应用于具有更详细签名的模块(此处为 struct type t = ForWrapp end
),它也会被强制降低为 T
并且构造函数信息会丢失。
我有
module type T = sig
type t
end
和
module Make (TypeProvider : T) = struct
include TypeProvider
type d = Wrapped of t
end
和
module Test = struct
include Make (struct type t = ForWrap end)
let f = function | Wrapped ForWrap -> ()
end
我想象编译后测试像
module Test = struct
type t = ForWrap
type d = Wrapped of t
let f = function | Wrapped ForWrap -> ()
end
但实际上,它不是可编译的代码。 OCaml 说我:
module Test = struct
include Make (struct type t = ForWrap end)
let f = function | Wrapped ForWrap -> ()
^^^^^^^
Error: Unbound constructor ForWrap
end
我不明白为什么。 我的解决方案有什么问题?
让我们看看Make (struct type t = ForWrapp end)
的签名:
module M = Make(struct type t = ForWrapp end)
ocamlc -c -i xxx.ml
显示此模块的签名:
module M : sig
type t
type d = Wrrapped of t
end
请注意,构造函数 ForWrapp
在结果模块中不可用。这就是您的代码不进行类型检查的原因。
为什么构造函数不见了?这是因为函子 Make
的参数签名是 T
。 T
定义了一个抽象类型 t
。即使您将 Make
应用于具有更详细签名的模块(此处为 struct type t = ForWrapp end
),它也会被强制降低为 T
并且构造函数信息会丢失。