我有以下代码:
module Test : sig
type +'a t
val make : int -> [< `a | `b] t
end = struct
type 'a t = Foo of int | Bar of string
let make = function
| 0 -> (Foo 0 : [`a] t)
| _ -> (Bar "hi" : [`a] t)
end
您可能会注意到,抽象类型'a t
在其类型参数中被声明为协变'a
,而make
构造函数被声明为返回多态变体 case或的子类型。a
b
在我的实现中make
,返回子类型[a] t
仍应遵循协方差规则,因为子类型位于返回类型位置。
但是,我收到以下错误:
Error: Signature mismatch:
...
Values do not match:
val make : int -> [ `a ] t
is not included in
val make : int -> [< `a | `b ] t
File ".../cov.ml", line 3, characters 3-34:
Expected declaration
File ".../cov.ml", line 7, characters 7-11:
Actual declaration
关于如何让 OCaml 相信该make
函数确实返回了 的有效子类型的任何建议[a | b] t
?