I'm trying to resolve an implicit then use the type stored in it to resolve a second implicit.
Here is the code:
sealed trait ReturnTypeRetriever[T] {
type ReturnType
}
object ReturnTypeRetrievers {
implicit val expl = new ReturnTypeRetriever[ExplorationStreamFilter] {
type ReturnType = SimpleFilter[Context, Context]
}
}
sealed trait Retriever[T, +R] {
def get(config: Config): R //SimpleFilter[Context, Context]
}
object Retrievers {
// implementation here for T=ExplorationStreamFilter and R=SimpleFilter[Context, Context]
implicit val expl = new Retriever[ExplorationStreamFilter, SimpleFilter[Context, Context]] {
override def get(config: Config) = {..}
}
// putting it all together
def getOrEmpty[A](config: Config)(implicit evret: ReturnTypeRetriever[A]) = {
val ev = implicitly[Retriever[A, evret.ReturnType]] <-- ERROR 1: cannot find implicit defined above ^
ev.get(config)
}
}
Calling it like this:
lazy val exploration = getOrEmpty[ExplorationStreamFilter](config) <--- ERROR 2: cannot find implicit here either
Compiling shows 2 errors:
ERROR 1 message: could not find implicit value for parameter e: Retriever[A,evret.ReturnType]
[error] val ev = implicitly[Retriever[A, evret.ReturnType]]
ERROR 2 message: could not find implicit value for parameter evret: ReturnTypeRetriever[ExplorationStreamFilter]
[error] lazy val exploration = getOrEmpty[ExplorationStreamFilter](config)
Why can't the compiler find the implicit? What's a solution for this?
This seems to work.
It uses the Aux pattern, and the Partially-applied trick.
(I do not understand what is the purpose of the
ReturnTypeRetrievertrait. But I leave it to preserve the original problem)