A very basic Type check fails in F# ... why?

f#, f#-3.0, f#-interactive

Solution

The plain answer is, do so:

if (x :> obj) :? Test then printfn "true" else printfn "false"

This issue comes because of the implementation of DUs (using internal classes and tags) and the limitation of F#'s type system (which does not acknowledge the implementation).

As you saw, the type of `x` is `FSI_0001+Test+Age`, and F# does not recognize that as a sub-type of `Test`.

Problem

I wrote this code ``` type Test = | Age of int | Name of string;; let x = Age(10);; if (x.GetType() = typeof<Test>) then printfn "true" else printfn "false";; ``` The code prints false. But that puzzles me because isn't Age of type Test? Also, is there a better way to compare types in F# the `.GetType() = typeof<>` is very long. I tried `:?` but I think that's for typecasting rather than comparing types.

Original source