shapeless

Lazy

trait Lazy[+T] extends Serializable

Wraps a lazily computed value. Also circumvents cycles during implicit search, or wrong implicit divergences as illustrated below, and holds the corresponding implicit value lazily.

The following implicit search sometimes fails to compile, because of a wrongly reported implicit divergence,

case class ListCC(list: List[CC])
case class CC(i: Int, s: String)

trait TC[T]

object TC {
  implicit def intTC: TC[Int] = ???
  implicit def stringTC: TC[String] = ???
  implicit def listTC[T](implicit underlying: TC[T]): TC[List[T]] = ???

  implicit def genericTC[F, G](implicit
    gen: Generic.Aux[F, G],
    underlying: TC[G]
  ): TC[F] = ???

  implicit def hnilTC: TC[HNil] = ???

  implicit def hconsTC[H, T <: HList](implicit
    headTC: TC[H],
    tailTC: TC[T]
  ): TC[H :: T] = ???
}

implicitly[TC[ListCC]] // fails with: diverging implicit expansion for type TC[ListCC]

This wrongly reported implicit divergence can be circumvented by wrapping some of the implicit values in Lazy,

case class ListCC(list: List[CC])
case class CC(i: Int, s: String)

trait TC[T]

object TC {
  implicit def intTC: TC[Int] = ???
  implicit def stringTC: TC[String] = ???
  implicit def listTC[T](implicit underlying: TC[T]): TC[List[T]] = ???

  implicit def genericTC[F, G](implicit
    gen: Generic.Aux[F, G],
    underlying: Lazy[TC[G]] // wrapped in Lazy
  ): TC[F] = ???

  implicit def hnilTC: TC[HNil] = ???

  implicit def hconsTC[H, T <: HList](implicit
    headTC: Lazy[TC[H]], // wrapped in Lazy
    tailTC: TC[T]
  ): TC[H :: T] = ???
}

implicitly[TC[ListCC]]

When looking for an implicit Lazy[TC[T]], the Lazy.mkLazy macro will itself trigger the implicit search for a TC[T]. If this search itself triggers searches for types wrapped in Lazy, these will be done only once, their result put in a lazy val, and a reference to this lazy val will be returned as the corresponding value. It will then wrap all the resulting values together, and return a reference to the first one.

E.g. with the above example definitions, when looking up for an implicit TC[ListCC], the returned tree roughly looks like

TC.genericTC(
Generic[ListCC], // actually, the tree returned by Generic.materialize, not written here for the sake of brevity
Lazy {
  lazy val impl1: TC[List[CC] :: HNil] = TC.hconsTC(
    Lazy(impl2),
    TC.hnilTC
  )
  lazy val impl2: TC[List[CC]] = TC.listTC(TC.genericTC(
    Generic[CC], // actually, the tree returned by Generic.materialize
    Lazy(impl1)  // cycles to the initial TC[List[CC] :: HNil]
  ))

  impl1
}
)
Annotations
@implicitNotFound( ... )
Linear Supertypes
Serializable, Serializable, AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Lazy
  2. Serializable
  3. Serializable
  4. AnyRef
  5. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract val value: T

Concrete Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  7. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. def flatMap[U](f: (T) ⇒ Lazy[U]): Lazy[U]

  12. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  13. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  14. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  15. def map[U](f: (T) ⇒ U): Lazy[U]

  16. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  17. final def notify(): Unit

    Definition Classes
    AnyRef
  18. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  19. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  20. def toString(): String

    Definition Classes
    AnyRef → Any
  21. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  22. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  23. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from Serializable

Inherited from Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped