Enumerating constructors of a type

Hello all,

Given a type, say

type color = Red | Green | Blue | White

Is there a way to enumerate the type constructors of color, which is [Red; Green; Blue; White] without manually writing these?

You can use a ppx (GitHub - janestreet/ppx_enumerate: Generate a list containing all values of a finite type) to derive this.

type color = Red | Green | Blue | White [@@deriving enumerate]

This will generate a function all_of_color which will equal [Red; Green; Blue; White]

4 Likes

Thanks, it worked well!

For everyone’s info, I added (preprocess (pps ppx_enumerate)) to lib/dune to use the extension.