force(p)
Return the result of evaluating the promise p
. A promise is created by delaying the evaluation of
an object with the delay
(~
) operator.
The first time a promise created by ~
is forced, it evaluates the delayed expression and its value is returned.
Force
also caches this value. Thereafter, each time the promise is forced, it returns the
cached value instead of re-evaluating the expression. In other words, the value of the expression
is memoized by force
.
~
and force
are typically used only in the absence of side effects, e.g., assignments, so
that the order of evaluation is unimportant.
The benefit of using ~
and force
is that some amount of computation might be avoided altogether if
it is delayed until absolutely required. Delayed evaluation may be used to construct conceptually infinite lists, or sequences.
let x = 10000000000000000000000
let p = ~{ showln("long computation..."); x }
force(p)
//> long computation...
// 10000000000000000000000
force(p)
// 10000000000000000000000