Last active
June 7, 2016 12:06
-
-
Save nikibobi/c42bd79ba4b8f27aee479449d340800f to your computer and use it in GitHub Desktop.
Get a seq of all the cases of union type in F#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Microsoft.FSharp.Reflection | |
let cases<'a> = seq { | |
for case in FSharpType.GetUnionCases typeof<'a> do | |
yield (FSharpValue.MakeUnion(case, [||]) :?> 'a) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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