F# Resources

You are here: main » core » option


|

Meta

Table of Contents

Option type

F# option type is often used to replace null values from other languages (i.e. when a function might return “nothing”). However, this is much safer. For instance, Map.tryfind is used to lookup an element in the map. It returns either “None”, or “Some value”.

Examples

> let li = [Some 10; Some 5; None; Some -12; None];;
val li : int option list

> List.map (fun x -> defaultArg x 3) li;;
val it : int list = [10; 5; 3; -12; 3]

> List.map (Option.map abs) li;;
val it : int option list = [Some 10; Some 5; None; Some 12; None]

> li |> List.map Option.length |> Seq.sum;;
val it : int = 3

Note: F# interactive sometimes print “null”, instead of “None”.

See also