[英]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 -> ()
^^^^^^^
錯誤:未綁定的構造函數ForWrap
end
我無法理解為什么。 我的解決方案有什么問題?
讓我們看一下Make (struct type t = ForWrapp 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
並且構造函數信息將丟失。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.