Jul 8, 2011

f# essential - part 13 - pattern matching over .net type

f# use unique syntax to match type like the following,

let divide1 x y =
   try
      Some (x / y)
   with
      | :? System.DivideByZeroException -> printfn "Division by zero!"; None

let result1 = divide1 100 0

let recognizeType (item : obj) = 
    match item with 
    | :? System.Int32 -> printfn "An integer" 
    | :? System.Double -> printfn "A double" 
    | :? System.String -> printfn "A string" 
    | _ -> printfn "Unknown type"