Search code examples
typesf#keywordlanguage-comparisons

F# equivalent of `is` keyword in C#?


My first F# day. If I have this:

let cat = Animal()

Now how do I check at later stage if cat is Animal?

In C#

bool b = cat is Animal;

In F#?


Solution

  • @ildjarn deserves the credit here for answering first, but I'm submitting the answer here so it can be accepted.

    The F# equivalent of the C# is keyword is :?. For example:

    let cat = Animal()
    if cat :? Animal then
        printfn "cat is an animal."
    else
        printfn "cat is not an animal."