Table of contents
TOC
Collapse the table of content
Expand the table of content

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

Den Delimarsky|Last Updated: 8/18/2017
|
1 Contributor

Forces the execution of this value and returns its result. Same as System.Lazy.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)

Syntax

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

// Usage:
lazy.Force ()

Return Value

The value of the Lazy object.

Example


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 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

Version Information

F# Core Library Versions

Supported in: 2.0

See Also

Control.LazyExtensions Module (F#)

Lazy Computations (F#)

© 2019 Microsoft