Udostępnij za pośrednictwem


Lazy.Force<'T> Extension Method (F#)

Forces the execution of this value and returns its result. Same as Value. Mutual exclusion is used to prevent other threads from also computing the value.

Namespace/Module Path: Microsoft.FSharp.Control.LazyExtensions

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

// Signature:
type System.Lazy with
  member Force : unit -> 'T

// Usage:
lazy.Force ()

Return Value

The value of the Lazy object.

Example

The following code illustrates the use of the Force extension method.

let lazyFactorial n = Lazy.Create (fun () ->
    let rec factorial n =
        match n with
        | 0 | 1 -> 1
        | n -> n * factorial (n - 1)
    factorial n)
let printLazy (lazyVal:Lazy<int>) =
    if (lazyVal.IsValueCreated) then
        printfn "Retrieving stored value: %d" (lazyVal.Value)
    else
        printfn "Computing value: %d" (lazyVal.Force())
let lazyVal1 = lazyFactorial 12
let lazyVal2 = lazyFactorial 10
lazyVal1.Force() |> ignore
printLazy lazyVal1
printLazy lazyVal2

The output indicates that when Force is called to create the value for lazyVal1, the computed value is retrieved when printing the values.

Retrieving stored value: 479001600
Computing value: 3628800

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

Silverlight

Supported in: 3

See Also

Reference

Control.LazyExtensions Module (F#)

Lazy<T>

Lazy Computations (F#)