Adapting parameter of implicit method failed

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]


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?


Hey @zhranklin, this looks like the perfect question for our Scala Users instance or Stack Overflow! This forum is for people interested in the evolution of the language and the tooling around it, so that could explain the lack of answers.

If you haven’t solved this problem yet, I encourage you to try luck with those other platforms :slight_smile: .


Thanks @jvican . But I think it’s defect of the scala compiler. I have debugged the compiler and I think the compiler should be improved. I’ve showed there’s logic problem (I think it’s a problem) during type inference for implicits.