class Cov[+A]
class Inv[A]
class Simple
object TestDMCreationContext extends App {
implicit val t1: Inv[Simple] = null
implicit val t2: Inv[String] = null
implicit def gen[E](implicit inv: Inv[E]): Cov[E] = new Cov[E]
implicitly[Cov[Simple]]
}
The code below fails. When debugging the compiler with that code, I found something:
Because the Cov[A] is covariant to A, when inferring the argument of gen[E], the compiler doesn’t know the actual type of E, both t1 and t2 are inferred, which cause ambiguous error.
In my opinion, to fix this, t2 shouldn’t be inferred because it will eventually fails the implicit method adaption(in that way, the WildCardType should contain type bounds), or the compiler should recover the ambiguous error.
If I change Inv[A] to Inv[+A], it’s fixed because the compiler will assign the max match(which is Simple
) to E. But in some case such as classTag materialization, the code can’t be modified.
How should I solve this problem?