Lazy expressions are expressions that are not evaluated immediately but are evaluated when the result is needed.
The basic syntax of the laze expressions is as follows.
let identifier = lazy ( expression )
expression
is a code that is evaluated only when a result is required, and identifier
is a value that stores the result.Lazy<'T>
, where the actual type used for 'T
is determined from the result of the expression.To force the expressions to be performed, you will need to call the Force
method.
Force
method causes the execution to be performed only one time.Force
return the same result but do not execute any code.The following example shows the usage of lazy expressions and the use of the Force
method.
let input = 39.6
let result = lazy (input * 10.0/12.0)
printfn "%f" (result.Force())
In the above code, the type of result is Lazy<float>
, and the Force
method returns a float
.