Option Module
Hosted interpreter permissions: Every function and value on this page requires no host permissions. Higher-order functions still enforce the requirements of their callbacks. These checks do not apply to CLI execution or compilation. See Host permissions.
option is a native type with two cases: Some value and None.
Option.defaultValue : 'a -> 'a option -> 'a
None
Returns option value or a fallback.
let port = Some 8080 |> Option.defaultValue 80
Option.defaultWith : (unit -> 'a) -> 'a option -> 'a
None
Computes fallback lazily.
let value = None |> Option.defaultWith (fun () -> 42)
Option.isNone : 'a option -> bool
None
Returns true when option is None.
let missing = None |> Option.isNone
Option.isSome : 'a option -> bool
None
Returns true when option is Some.
let present = Some "x" |> Option.isSome
Option.map : ('a -> 'b) -> 'a option -> 'b option
None
Transforms value inside Some.
let upper = Some "fscript" |> Option.map String.toUpper
Pure support additions
Option.bind
None
Option.bind : ('a -> 'b option) -> 'a option -> 'b option
Calls the binder only for Some; its option result is returned directly.
Option.bind Int.tryParse (Some "42")
// Some 42
Option.flatten
None
Option.flatten : 'a option option -> 'a option
Removes one nested option layer. Both None and Some None produce None.
Option.flatten (Some (Some 2))
// Some 2
Option.filter
None
Option.filter : ('a -> bool) -> 'a option -> 'a option
Retains a present value only when the predicate is true. A missing value makes no callback call.
Option.filter (fun x -> x>1) (Some 1)
// None
Option.orElse
None
Option.orElse : 'a option -> 'a option -> 'a option
Uses the fallback when the final argument is None. Arguments are evaluated eagerly; use orElseWith for lazy fallback computation.
Option.orElse (Some 2) None
// Some 2
Option.orElseWith
None
Option.orElseWith : (unit -> 'a option) -> 'a option -> 'a option
Calls the fallback with unit only when the final argument is None.
Option.orElseWith (fun () -> Some 2) None
// Some 2
Option.map2
None
Option.map2 : ('a -> 'b -> 'd) -> 'a option -> 'b option -> 'd option
Calls the mapper only when both options are present; otherwise returns None.
Option.map2 (fun a b -> a+b) (Some 1) (Some 2)
// Some 3
Option.toList
None
Option.toList : 'a option -> 'a list
Returns a singleton list for Some or an empty list for None.
Option.toList (Some 1)
// [1]