samedi 8 juin 2019

F#: How to get the type of an instance of an empty discriminated union case using Reflection?

I read the answers of those questions:

But I found out something surprising about the underlying type of discriminated unions:

type Union1 =
    | A
    | B 

type Union2 =
    | A
    | B of int

[<EntryPoint>]
let main argv =
    printfn "%A" (Union1.A.GetType())
    printfn "%A" (Union1.B.GetType())

    printfn "%A" (Union2.A.GetType())
    printfn "%A" (Union2.B(32).GetType())
    0

Program+Union1
Program+Union1
Program+Union2+_A
Program+Union2+B

Hence my question how I can discriminate a case based on the type when a case is empty?





Aucun commentaire:

Enregistrer un commentaire