I’d like to write a module (in Ocaml 3.12) able to define a Variant type as an aggregation of existing different types
It could be 0 to N types, so a variable list or set
it could look like this :
type newtype = Type0 of type0 | Type1 of type1 | ... | TypeN of typeN
Of course I want to factorize its creation
First I tried to create a module ‘Composite’ parameterized by functors :
module Composite ( T0 : sig type t end ) ( T1 : sig type t end ) =
struct
type t = T0.t | T1.t
end
First difficulty : how can I pass a variable list of functors to the ‘Composite’ module ?
Is this a good way to procede ?
edit1 : Variant allow to define a XOR type definition (it’s either T0 ot T1 but not both); how can I define an OR type definition (can be T0 or T1 or both )?
If want a “flat” composite type and get the union of constructors, the only way is to use polymorphic variants (as stated by @lukstafi in the comments), but in this case t1 and t2 cannot be abstract:
If you really want to use modules you have to loose your flat representation and thus you will have a disjoint union: