Type checking, query for parent or subtypes

Hi, how do I check in the below if a Square is also a Rectangle, I know they are dicriminated but can I query the hierarchical relationship?

type Shape =
    | Flat
        | Rectangle
            | Square
        | Circle

Another question, how is the :? operator applied. Can it be used in these matters.

That type definition doesn’t describe the hierarchy you’re talking about. Perhaps you know that already and it was meant as illustrative pseudo-code. But if that’s the case, it’s not clear to me what you’re asking here. Can you provide a minimal working code sample that sets up the problem?

Hi Prash,

Thanks for taking time. If I instantiate a Square case, is there a way to test if that Square is conceptually child of case Rectangle. Does that clarify? How do I test for this IS-A relationship. Is it possible with DU’s

Best Regards Jesper.

A single DU type definition can contain multiple cases which are all siblings at the same level. It only creates one type. Your example shows them indented at different levels but that indentation is misleading and they should all be in line. If you want to use a DUs to create a hierarchy like this you need multiple DUs, which means multiple types:

type Rectangle =
    | Square

type Flat =
    | Rectangle of Rectangle
    | Circle

type Shape =
    | Flat of Flat

Now when we have a Rectangle it means we have a Rectangle type. In normal statically typed programming, the compiler and the programmer would know the type of any given value, so there is no need to do a type test at runtime.

I hope that helps to clarify. It’s useful to understand that there’s a difference between a DU type and a DU case: The DU itself is the type, and the case is a value that has that type.