In this video, I document my understanding of F#’s Type Test Pattern:

 

Here’s a snippet:
open System

let requestFromIO number =

try
let result = 100 / number
Some “Operation Suceeded”

with
| “:?” DivideByZeroException -> Some “DivideByZeroException”
| “:?” AccessViolationException -> Some “AccessViolationException”
| _ -> None

let result = requestFromIO 0

Here’s another snippet
open System

let reportError (error:Exception) =
match error with
| “:?” DivideByZeroException -> “DivideByZeroException”
| “:?” FieldAccessException -> “FieldAccessException”
| _ -> “IDK”

let result = reportError (FormatException())

 

Advertisements