OCaml functor with polymorphic variant
type t = [> `Foo]
invalid because it [> `Foo]
is an open type and implicitly contains a type variable. The definition is rejected in the same way that the definition of the next type is rejected because the RHS has a type variable that is not quantified in the LHS:
type t = 'a list
You have to close it:
type t = [ `Foo ]
or quantify a variable of type:
type 'a t = [> `Foo] as 'a
which is equivalent
type 'a t = 'a constraint 'a = [> `Foo]
source to share
This seems to work:
module F ( M : sig type base_t = [ 'Foo ] type 'a t = [> base_t] as 'a end) = struct type t = [ M.base_t | 'Bar ] M.t end
M.base_t
closed, but Mt('a)
polymorphic. F
builds Mt
using M.base_t
advanced with 'Bar
.
Here is a mindml try link that includes the above code snippet in OCaml and ReasonML syntax and proves that it compiles.
source to share