| id | stdlib-option |
|---|---|
| title | Option Module |
| slug | /stdlib/option |
option is a native type with two cases: Some value and None.
Returns option value or a fallback.
let port = Some 8080 |> Option.defaultValue 80Computes fallback lazily.
let value = None |> Option.defaultWith (fun () -> 42)Returns true when option is None.
let missing = None |> Option.isNoneReturns true when option is Some.
let present = Some "x" |> Option.isSomeTransforms value inside Some.
let upper = Some "fscript" |> Option.map String.toUpperOption.bind : ('a -> 'b option) -> 'a option -> 'b optionCalls the binder only for Some; its option result is returned directly.
Option.bind Int.tryParse (Some "42")
// Some 42Option.flatten : 'a option option -> 'a optionRemoves one nested option layer. Both None and Some None produce None.
Option.flatten (Some (Some 2))
// Some 2Option.filter : ('a -> bool) -> 'a option -> 'a optionRetains a present value only when the predicate is true. A missing value makes no callback call.
Option.filter (fun x -> x>1) (Some 1)
// NoneOption.orElse : 'a option -> 'a option -> 'a optionUses the fallback when the final argument is None. Arguments are evaluated eagerly; use orElseWith for lazy fallback computation.
Option.orElse (Some 2) None
// Some 2Option.orElseWith : (unit -> 'a option) -> 'a option -> 'a optionCalls the fallback with unit only when the final argument is None.
Option.orElseWith (fun () -> Some 2) None
// Some 2Option.map2 : ('a -> 'b -> 'd) -> 'a option -> 'b option -> 'd optionCalls the mapper only when both options are present; otherwise returns None.
Option.map2 (fun a b -> a+b) (Some 1) (Some 2)
// Some 3Option.toList : 'a option -> 'a listReturns a singleton list for Some or an empty list for None.
Option.toList (Some 1)
// [1]