Generated sum type constructor functions?

Suppose you have

type t =
  | Foo
  | Bar of int
  | Baz of string

I understand in some of OCaml’s cousins you can do this, but in OCaml you specifically cannot say Bar and receive a curried function that takes int and will yield your value Bar int.

So, to get the behavior I want, I’d have to write:

let foo = Foo
let bar n = Bar n
let baz s = Baz s

and say bar instead of Bar.

Fine. That’s easy enough, though I was wondering if there was a ppx rewriter that will generate these?

1 Like

I think GitHub - janestreet/ppx_variants_conv: Generation of accessor and iteration functions for ocaml variant types does what you’re looking for.

1 Like

What! Yes, this does exactly what I want. I can’t believe it’s been part of ppx_jane this whole time.

I guess I should look through all of these :thinking:

ppx_enumerate! :star_struck:

1 Like