我有一个问题,为什么我在F#中获得某些结果.我有以下代码......
let lHelloWorld = lazy(printfn "Lazy Hello World"; 30+30)
let aHelloWorld = (printfn "Active Hello World"; 30+30)
printfn "lazy value is %d" lHelloWorld.Value
printfn "lazy value is %d" lHelloWorld.Value
printfn "active value is %d" aHelloWorld
printfn "active value is %d" aHelloWorld
Run Code Online (Sandbox Code Playgroud)
我的输出如下......
Active Hello World
Lazy Hello World
lazy value is 60
lazy value is 60
active value is 60
active value is 60
Run Code Online (Sandbox Code Playgroud)
我无法理解的是......为什么在懒惰的hello字之前显示活动的hello world的printfn?我本来期望"懒人世界"在"活跃你好世界"之前出现过?
如果有人可以帮助解释这一点,将不胜感激.
这是我的注释说明
// Here we go...
let lHelloWorld = lazy(printfn "Lazy Hello World"; 30+30)
// Ok, we defined a value called lHelloWorld. The right hand side is
// a lazy(...), so we don't evaluate the ..., we just store it in a
// System.Lazy object and assign that value to lHelloWorld.
let aHelloWorld = (printfn "Active Hello World"; 30+30)
// Ok, we're defining a value called aHelloWorld. The right hand side is
// a normal expression, so we evaluate it it right now. Which means we
// print "Active Hello World", and then compute 30+30=60, and assign the
// final result (60) to aHelloWorld.
// Note that neither of the previous two entities are functions.
// Now we have some effects:
printfn "lazy value is %d" lHelloWorld.Value
// Calling .Value on an object of type System.Lazy will either
// - force the value if it has not yet been evaluated, or
// - return the cached value if it was previously evaluated
// Here we have not yet evaluated it, so we force the evaluation,
// which prints "Lazy Hello World" and then computes 30+30=60, and then
// stores the final 60 value in the lHelloWorld's cache. Having evaluated
// the arguments to the printfn on this line of code, we can now call that
// printfn, which prints "lazy value is 60".
printfn "lazy value is %d" lHelloWorld.Value
// This time, calling .Value just fetches the already-computed cached value,
// 60, and so this just prints "lazy value is 60".
printfn "active value is %d" aHelloWorld
// aHelloWorld has type 'int'. Its value is 60. This is trivial,
// it prints "active value is 60".
printfn "active value is %d" aHelloWorld
// Ditto.
Run Code Online (Sandbox Code Playgroud)