To reduce compile times of my project, I'm caching certain type classes that are resolved by implicit lookups. This appears somewhat cumbersome though, because the straight forward implementation does not work:
scala> implicit val x: String = implicitly[String]
x: String = null
The implicit lookup considers its own, uninitialized definition as a valid implementation. A lazy val
would blow the stack with infinite recursion. Therefore I'm currently handling it in this fashion:
implicit val x: String = cache.x
object cache {
val x: String = implicitly[String]
}
But this makes it overly complicated, and the cache-definitions can not make use of other cached type classes easily (since they are not implicit).
Also, hiding the value itself from scope does unfortunately not work.
scala> :pas
// Entering paste mode (ctrl-D to finish)
object scope {
implicit val x: String = {
import scope.{ x => _ }
implicitly[String]
}
}
// Exiting paste mode, now interpreting.
defined object scope
scala> scope.x
res0: String = null
Is there a more elegant way to achieve an implicit resolution cache?
cachedImplicit
macro? It's very similar to what you're doing. – Ararat