Skip to content

Instantly share code, notes, and snippets.

@nikibobi
Last active June 7, 2016 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikibobi/c42bd79ba4b8f27aee479449d340800f to your computer and use it in GitHub Desktop.
Save nikibobi/c42bd79ba4b8f27aee479449d340800f to your computer and use it in GitHub Desktop.
Get a seq of all the cases of union type in F#
open Microsoft.FSharp.Reflection
let cases<'a> = seq {
for case in FSharpType.GetUnionCases typeof<'a> do
yield (FSharpValue.MakeUnion(case, [||]) :?> 'a)
}
type Animal = Cat | Dog | Mouse | Pig | Chicken
let animals = cases<Animal>
animals |> Seq.iter (printfn "%A")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment