Created
May 7, 2015 05:22
-
-
Save SteveGilham/58c18e5dedc5b4c95d3d to your computer and use it in GitHub Desktop.
Getting the types in a union type.
This file contains hidden or 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 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