A Lazy[A] is a computation of an A that runs when force is first called. The schema module uses it where parts of a structure must refer to parts that are not built yet, as in a recursive type: the methods of a ReflectTransformer return their nodes as Lazy values, and type class derivation pairs the binding of each node with a Lazy instance.
map, flatMap and the other combinators build a new Lazy without running anything. force runs the whole chain with an explicit stack, so a long chain does not overflow the stack, and caches the result, or the exception, in the Lazy it is called on. The cache belongs to that instance: a Lazy that is part of another one's chain runs its computation again when the other one is forced, even if it was forced before.
var evaluations = 0
val title: Lazy[String] = Lazy {
evaluations += 1
"Zionomicon"
}
val before = title.isEvaluated // false
val first = title.force // Zionomicon
val second = title.force // Zionomicon
val after = (title.isEvaluated, evaluations) // (true,1)
Attributes
- See also
- Companion
- object
- Source
- Lazy.scala
- Graph
-
- Supertypes
Members list
Value members
Concrete methods
Returns a Lazy that runs this one, discards its result and evaluates b.
Returns a Lazy that runs this one, discards its result and evaluates b.
Attributes
- Source
- Lazy.scala
Returns a Lazy that runs this one and, when it throws a non-fatal exception, runs the Lazy that f returns for that exception instead.
Returns a Lazy that runs this one and, when it throws a non-fatal exception, runs the Lazy that f returns for that exception instead.
Attributes
- Source
- Lazy.scala
Returns a Lazy that runs this one and then forces finalizer, whether this one returns or throws a non-fatal exception. The exception is thrown again after finalizer runs; an exception from finalizer replaces it.
Returns a Lazy that runs this one and then forces finalizer, whether this one returns or throws a non-fatal exception. The exception is thrown again after finalizer runs; an exception from finalizer replaces it.
Attributes
- Source
- Lazy.scala
Forces this Lazy and that one, and compares their results.
Forces this Lazy and that one, and compares their results.
Attributes
- Definition Classes
-
Any
- Source
- Lazy.scala
Returns a Lazy that runs this one, then the Lazy that f returns for its result.
Returns a Lazy that runs this one, then the Lazy that f returns for its result.
Attributes
- Source
- Lazy.scala
Returns a Lazy that runs this one, then the Lazy it returns.
Runs this computation and returns its result, or returns the cached result of an earlier call. A non-fatal exception is cached as well: later calls throw it again without running the computation. A null result is not cached, so the next call runs the computation again.
Runs this computation and returns its result, or returns the cached result of an earlier call. A non-fatal exception is cached as well: later calls throw it again without running the computation. A null result is not cached, so the next call runs the computation again.
Attributes
- Source
- Lazy.scala
Forces this Lazy and returns the hash code of its result.
Forces this Lazy and returns the hash code of its result.
Attributes
- Definition Classes
-
Any
- Source
- Lazy.scala
Returns true once force has returned a non-null result or thrown a non-fatal exception.
Returns true once force has returned a non-null result or thrown a non-fatal exception.
Attributes
- Source
- Lazy.scala
Returns a Lazy that runs this one and applies f to its result.
Returns Lazy() before force, and Lazy( followed by the cached result and ) after it (Lazy(null) after a failure).
Returns Lazy() before force, and Lazy( followed by the cached result and ) after it (Lazy(null) after a failure).
Attributes
- Definition Classes
-
Any
- Source
- Lazy.scala
Returns a Lazy that runs this one and discards its result.
Returns a Lazy that runs this one, then that, and pairs their results.