Eval is a monad which controls evaluation.
This type wraps a value (or a computation that produces a value) and can produce it on command via the .value
method.
There are three basic evaluation strategies:
- Now: evaluated immediately - Later: evaluated once when value is needed - Always: evaluated every time value is needed
The Later and Always are both lazy strategies while Now is eager. Later and Always are distinguished from each other only by memoization: once evaluated Later will save the value to be returned immediately if it is needed again. Always will run its computation every time.
Eval supports stack-safe lazy computation via the .map and .flatMap methods, which use an internal trampoline to avoid stack overflows. Computation done within .map and .flatMap is always done lazily, even when applied to a Now instance.
It is not generally good style to pattern-match on Eval instances. Rather, use .map and .flatMap to chain computation, and use .value to get the result when needed. It is also not good style to create Eval instances whose computation involves calling .value on another Eval instance -- this can defeat the trampolining and lead to stack overflows.
Attributes
- Companion
- object
- Source
- Eval.scala
- Graph
-
- Supertypes
- Known subtypes
- Self type
-
Eval[A]