I am having a hard time understanding an error related to "could not find value for implicits" error. Here is a minimal example to highlight the error.
sealed trait BehaviourA
final case object A1 extends BehaviourA
final case object A2 extends BehaviourA
sealed trait BehaviourB
final case object B1 extends BehaviourB
final case object B2 extends BehaviourB
trait factory[O<:BehaviourA, R<:BehaviourB]{ ... }
// Define an implicit value for each combination of the two behaviours
object factory {
implicit val behaviour1 = new factory[A1, B1] { ... }
... (one for each Ai, Bj)
}
object genElement {
import factory._
def apply[O <: BehaviourA, R<:BehaviourB](...)(implicit c: factory[O, R])
}
When I try to call it from another method, however, it complains about no value found for implicit c in the genExample.
def callerMtd[T<:BehaviourA](){
genElement[T, B1](...)
}
or
def callerMtd[T<:BehaviourA, R<:BehaviourB](){
genElement[T, R](...)
}
If I call them directly like below, it is fine. Why can't I use type parameters in the caller method?
def callerMtd(){
genElement[A1, B2](...)
}
Even though the types are sealed and seemingly you provided all type class instances
(one for each Ai, Bj)
this does not cover all the possible cases specified by type bounds in
def callerMtd[T <: BehaviourA, R <: BehaviourB]
For example here are types that fit the bounds and yet you probably did not provide an instance for them
callerMtd[A1.type with A2.type, B1.type with B2.type]
For compiler to be sure it can summon an instance for factory[T, R]
where T <: BehaviourA
and R <: BehaviourB
you would have to instruct it how to generate it with corresponding implicit def
implicit def foo[T <: BehaviourA, R <: BehaviourB]