Having a disjoint union type like this:
type yolo = | A | B | C
Is it possible in OCaml to iterate/enumerate over each possible value of type yolo
without prior knowledge of how many/what these values are?
What I want to achieve is a function that takes a type and returns a list with all of its values. For example:
let yolos = extractor yolo
val yolos : yolo list = [A; B; C]
(I am looking for a way to implement extractor
function from the above snippet)
In F# my goal is achievable with something similar to this:
open Microsoft.FSharp.Reflection
type yolo =
| A
| B
| C
let yolos = FSharpType.GetUnionCases typeof<yolo>
Yes, it is possible. It looks like you're looking for Jane Street's ppx_enumerate
which does exactly this, even working with polymorphic types.