Ppx_variants_conv on mutually dependent types

I am trying to use Jane Street’s ppx_variants_conv (GitHub - janestreet/ppx_variants_conv: Generation of accessor and iteration functions for ocaml variant types) for a problem that includes mutually dependent types. Unfortunately, the ppx fails with a not supported error. Do we know whether there is a fundamental issue that prevents this type of rewriter to work on this scenario, and (if not) whether we can expect a support in the near future?

Or are there alternative options available out there?

For reference, this is the type of code I would like to write. Ignore the exact names, I did not want to use foo and bar.

type t =
  | Point of t'
  | Line of (int * int)
  [@@deriving variants]
and t' = Pair of (t * t)

Cheers, Alban.

I think it’s probably not too hard to add support recursive definitions in ppx_variants_conv, but for now, you can probably work around by unwrapping the recursion:

type 't t'' = | Point of 't | Line of int * int [@@deriving variants]
type t = t' t'' and t' = Pair of t * t

This is such a neat trick. Thanks for that, Stefan.

By the way, Pair of (t * t) and Pair of t * t are subtly different! This is, for example, explained here: ocaml - Is a multiple argument constructor ever useful over a single tuple argument constructor? - Stack Overflow.
The latter is generally preferred.

1 Like