Search code examples
interfacef#typescompilationtypechecking

How to check in F# whether object implements interface


Prototypical code in C#:

if(obj1 is ISomeInterface) {
   do_something
}

Code in F# that doesn't compile:

match obj1 with 
| :? ISomeInterface -> do_something
| _ -> ()

Solution

  • match box obj1 with 
    | :? ISomeInterface -> do_something
    | _ -> ()