Lazy

zio.blocks.schema.Lazy
See theLazy companion object
sealed trait Lazy[+A]

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
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

final def as[B](b: => B): Lazy[B]

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
final def catchAll[B >: A](f: Throwable => Lazy[B]): Lazy[B]

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
final def ensuring(finalizer: Lazy[Any]): Lazy[A]

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
final override def equals(that: Any): Boolean

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
final def flatMap[B](f: A => Lazy[B]): Lazy[B]

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
final def flatten[B](implicit ev: A <:< Lazy[B]): Lazy[B]

Returns a Lazy that runs this one, then the Lazy it returns.

Returns a Lazy that runs this one, then the Lazy it returns.

Attributes

Source
Lazy.scala
final def force: A

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
final override def hashCode: Int

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
final def isEvaluated: Boolean

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
final def map[B](f: A => B): Lazy[B]

Returns a Lazy that runs this one and applies f to its result.

Returns a Lazy that runs this one and applies f to its result.

Attributes

Source
Lazy.scala
final override def toString: String

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
final def unit: Lazy[Unit]

Returns a Lazy that runs this one and discards its result.

Returns a Lazy that runs this one and discards its result.

Attributes

Source
Lazy.scala
final def zip[B](that: Lazy[B]): Lazy[(A, B)]

Returns a Lazy that runs this one, then that, and pairs their results.

Returns a Lazy that runs this one, then that, and pairs their results.

Attributes

Source
Lazy.scala