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

更新:2010 年 5 月

调用本身来自于选项的选项值上的函数。

命名空间/模块路径:Microsoft.FSharp.Core.Option

程序集:FSharp.Core(在 FSharp.Core.dll 中)

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

// Usage:
bind binder option

参数

  • binder
    类型:'T -> 'U option

    一个函数,它从选项中获取类型 T 的值,并将其转换为包含类型 U 的值的选项。

  • option
    类型:'T option

    输入选项。

返回值

联编程序的输出类型的选项。

备注

表达式 Option.bind f inp 计算为 match inp with None -> None | Some x -> f x.

此函数在编译的程序集中名为 Bind。 如果从 F# 以外的语言中访问函数,或通过反射访问成员,请使用此名称。

示例

下面的代码阐释了 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

  

平台

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

版本信息

F# 运行时

受以下版本支持:2.0、4.0

Silverlight

受以下版本支持:3

请参见

参考

Core.Option 模块 (F#)

Microsoft.FSharp.Core 命名空间 (F#)

修订记录

Date

修订记录

原因

2010 年 5 月

添加了代码示例。

信息补充。