Just Code‎ > ‎

F# - Returning Nullable objects from a function

posted Dec 29, 2011, 3:09 PM by Peter Henell
"Some" is basically boxing the value while "None" is to be considered as null. In this example we box and return a int*int tuple if everything goes OK.
let GetNum x =
    try
        Some(100 / x, 500 / x)
    with
    | :? System.DivideByZeroException | :? System.OverflowException -> None


let f a = 
    match a with 
    | Some(tupl) -> printfn "%A" tupl
    | None -> printfn "An error must have occured since there is no value"

f (GetNum 0)
f (GetNum 20)
Comments