Lazy.Create<'T> 扩展方法 (F#)

更新:2010 年 5 月

创建一个在强制执行时的计算结果为给定函数的结果的延迟计算。

命名空间/模块路径: Microsoft.FSharp.Control.LazyExtensions

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

// Signature:
type System.Lazy with
  member static Create : Lazy<'T>

// Usage:
lazy.Create (creator)

参数

  • creator
    类型:unit -> 'T

    用于在需要时提供值的函数。

返回值

创建的 Lazy 对象。

示例

下面的代码阐释了 Create 的用法。

let lazyValue n = Lazy.Create (fun () ->
    let rec factorial n =
        match n with
        | 0 | 1 -> 1
        | n -> n * factorial (n - 1)
    factorial n)
let lazyVal = lazyValue 10
printfn "%d" (lazyVal.Force())

输出为 10 的阶乘。

  

平台

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

Silverlight

受以下版本支持:3

请参见

参考

Control.LazyExtensions 模块 (F#)

Lazy<T>

延迟计算 (F#)

修订记录

Date

修订记录

原因

2010 年 5 月

添加了代码示例。

信息补充。