Unfortunately, you cannot put record fields in the arguments of variant constructor declarations. You must define it separately:
type xy = { x : int; y : int }
type test = A of xy | B of xy
This is slightly memory inefficient since you need another memory block for xy
. Personally I do not mind it if the readability is greatly improved by the record.
However, I remember there was a proposal and a patch of having a record fields as variant constructors just like you have written:
type test = A of { x : int; y : int } | B of { x : int; y : int }
which does not require the extra block. But it is not in OCaml 4.01.0, and I am not sure the possibility of availability of this extension in future OCaml releases. Further comments are welcome.