我正在尝试使用模块/函子来进行更通用的代码设计。为了简化,我有两个接口:
module type T1 = sig type t end;;
module type T2 = sig type t end;;
我想T2.t
用一个基于T1.t
.
(* simple example, this one is accepted *)
module OK (T: T1) : (T2 with type t = T.t) = struct type t = T.t end;;
(* using a variant type, this one is rejected *)
module KO (T: T1) : (T2 with type t = X | Y of T.t) = struct
type t = X | Y of T.t
end
在后者中,我收到以下错误:
Unbound module X
Syntax error inside `module' after unclosed (, expected `.'
但是,如果我使用多态变体,它似乎被接受:
module OK2 (T: T1) : (T2 with type t = [ `X | `Y of T.t]) = struct
type t = [ `X | `Y of T.t ]
end
但我显然不明白为什么。将此类约束与变体一起使用的正确方法是什么?
PS:注意这个也是被拒的
module OK2 (T: T1) : (T2 with type t = [ `X | `Y of T.t]) = struct
type t = X | Y of T.t
end