final case class Cached[+T](value: T) extends AnyVal with Product with Serializable
Wraps a cached implicit T
.
Looking for an implicit Cached[T]
first triggers a look for an implicit T
, caches the resulting
tree, and returns it immediately and in subsequent look ups for an implicit Cached[T]
. Thus,
subsequent look ups do not trigger looking for an implicit T
, only returning the instance kept in
cache.
Beware that if the contexts in which two subsequent look ups are different, so that looking for a
T
in each of them doesn't return the same result, this change would be ignored by caching. Looking
for a Cached[T]
in the first context would put the implicit T
of this context in cache, and then
looking for a Cached[T]
in the second context would return the former instance from the first
context. E.g.
trait TC[T] { def msg: String } object First { implicit val tc: TC[Int] = new TC[Int] { val msg = "first" } def print() = println(implicitly[TC[Int]].msg) def printCached() = println(cached[TC[Int]].msg) } object Second { implicit val tc: TC[Int] = new TC[Int] { val msg = "second" } def print() = println(implicitly[TC[Int]].msg) def printCached() = println(cached[TC[Int]].msg) } First.print() Second.print() First.printCached() Second.printCached()
would print "first" then "second" (non cached TC[Int]
instances), then "first" twice (first instance, returned
the second time too through the cache).
- Alphabetic
- By Inheritance
- Cached
- Serializable
- Serializable
- Product
- Equals
- AnyVal
- Any
- Hide All
- Show All
- Public
- All
Instance Constructors
- new Cached(value: T)
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
##(): Int
- Definition Classes
- Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
getClass(): Class[_ <: AnyVal]
- Definition Classes
- AnyVal → Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- val value: T