I’m just beginning F# and haven’t really done functional programming since my programming languages class 15 years ago (exception being “modern” C#).
I’m looking at this F# snippet using LINQPad 4:
let add x y = x + y
let lazyPlusOne x = lazy add x 1
let e = lazyPlusOne 15
Dump e
let plusOne x = add x 1
let f = plusOne 15
Dump f
The output it produces is:
Lazy<Int32>
Value is not created.
IsValueCreated False
Value 16
16
I understand the lazy keyword to delay evaluation until needed, same as C# delayed execution.
What is the meaning of: “Value is not created” here?
If you use
lazykeyword to construct a lazy value (as in yourlazyPlusOnefunction), then the result is a value of typeLazy<int>. This represents a value of typeintthat is evaluated only when it is actually needed.I assume that
Dumpfunction tries to print the value including all its properties – when it starts printing, the value is not evaluated, soToStringmethod printsValue is not created.Then it iterates over other properties and when it accessesValue, the lazy value is evaluated (because its value is now needed). After evaluation, the property returns 16, which is then printed.You can replace
Dumpwith an F#-friendly printing function (or just use F# Interactive, which is extremely convenient way to play with F# inside Visual Studio with the usual IntelliSense, background error checking etec.)F#-friendly printing function like
printfn "%A"doesn’t access theValueproperty, so it doesn’t accidentally evaluate the value. Here is a snippet from F# Interactive: