Udostępnij za pośrednictwem


Option.bind<'T,'U> Function (F#)

Invokes a function on an optional value that itself yields an option.

Namespace/Module Path: Microsoft.FSharp.Core.Option

Assembly: FSharp.Core (in FSharp.Core.dll)

// Signature:
bind : ('T -> 'U option) -> 'T option -> 'U option

// Usage:
bind binder option

Parameters

  • binder
    Type: 'T -> 'U option

    A function that takes the value of type T from an option and transforms it into an option containing a value of type U.

  • option
    Type: 'T option

    The input option.

Return Value

An option of the output type of the binder.

Remarks

The expression Option.bind f inp evaluates to match inp with None -> None | Some x -> f x.

This function is named Bind in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.

Example

The following code illustrates the use of Option.bind.

let stringOpt1 = Some("Mirror Image")
let stringOpt2 = None
let reverse (string : System.String) =
    match string with
    | "" -> None
    | s -> Some(new System.String(string.ToCharArray() |> Array.rev))

let result1 = Option.bind reverse stringOpt1
printfn "%A" result1
let result2 = Option.bind reverse stringOpt2
printfn "%A" result2

Output

Some "egamI rorriM"
<null>

Platforms

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2

Version Information

F# Runtime

Supported in: 2.0, 4.0

Silverlight

Supported in: 3

See Also

Reference

Core.Option Module (F#)

Microsoft.FSharp.Core Namespace (F#)