Skip to content

Instantly share code, notes, and snippets.

@SteveGilham
Created May 7, 2015 05:22
Show Gist options
  • Save SteveGilham/58c18e5dedc5b4c95d3d to your computer and use it in GitHub Desktop.
Save SteveGilham/58c18e5dedc5b4c95d3d to your computer and use it in GitHub Desktop.
Getting the types in a union type.
open System
open Microsoft.FSharp.Reflection
let GetUnionTypes (t:Type) =
seq { yield t
if FSharpType.IsUnion t
then yield! FSharpType.GetUnionCases t
// implementation detail leaks here -- nested type name
|> Seq.map (fun x -> (string t) + "+" + x.Name)
|> Seq.map Type.GetType }
type expr = Num of int
| Operation of (expr * expr)
let baseType = typeof<expr>
GetUnionTypes baseType
|> Seq.iter (printfn "%A")
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment