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 {
implicitval 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 {
implicitval 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).
Wraps a cached implicit
T
.Looking for an implicit
Cached[T]
first triggers a look for an implicitT
, caches the resulting tree, and returns it immediately and in subsequent look ups for an implicitCached[T]
. Thus, subsequent look ups do not trigger looking for an implicitT
, 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 aCached[T]
in the first context would put the implicitT
of this context in cache, and then looking for aCached[T]
in the second context would return the former instance from the first context. E.g.would print "first" then "second" (non cached
TC[Int]
instances), then "first" twice (first instance, returned the second time too through the cache).