Reputation: 3650
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
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
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
Reputation: 25516
You code should be
let lazyValue = lazy ( 2 + 2 )
let actualValue = lazyValue.Force()
printfn "%i" actualValue
Upvotes: 2