Trait/Object

shapeless

Lazy

Related Docs: object Lazy | package shapeless

Permalink

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
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Lazy
  2. Serializable
  3. Serializable
  4. AnyRef
  5. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract val value: T

    Permalink

Concrete Value Members

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

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

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

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

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

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

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

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

    Permalink
  10. final def getClass(): Class[_]

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

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

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

    Permalink
  14. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  15. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  16. final def notifyAll(): Unit

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

    Permalink
    Definition Classes
    AnyRef
  18. def toString(): String

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

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

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

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from Serializable

Inherited from Serializable

Inherited from AnyRef

Inherited from Any

Ungrouped