roast_soul
roast_soul

Reputation: 3650

A F# example on book, but I can't even compile it

I'm reading the book 'Beginning F#', There's a short list for example code, to demonstrate the lazy evaluation as follows:

lazyValue = lazy ( 2 + 2 )
let actualValue = Lazy.force lazyValue
printfn "%i" actualValue

It seems easy, but there's a error to me, say that function force isn't defined???

I'm confused about that. Searching from the msdn, it seems no answer.

Anyone can tell me what happened??

Upvotes: 2

Views: 241

Answers (3)

pad
pad

Reputation: 41290

For some reason, Lazy.force is in F# PowerPack now.

Since this function doesn't have dependency, I suggest you to copy it from F# PowerPack for convenient use:

module Lazy =
    let force (x: Lazy<'T>) = x.Force()

Upvotes: 8

t0yv0
t0yv0

Reputation: 4724

The author wrote Lazy.force because the author probably used OCaml before. F# gravitated from OCaml style API to C# style API over time. Now people write x.Value or x.Force(). instead.

Upvotes: 4

John Palmer
John Palmer

Reputation: 25516

You code should be

    let lazyValue = lazy ( 2 + 2 )
    let actualValue = lazyValue.Force()
    printfn "%i" actualValue

Upvotes: 2

Related Questions