问题
I am trying to diagnose a compiler error which is caused by diverging implicit expansion errors.
Here is what I am seeing:
❯ scala
Welcome to Scala 2.13.1 (OpenJDK 64-Bit Server VM, Java 1.8.0_252).
Type in expressions for evaluation. Or try :help.
scala> :settings -Xlog-implicits
scala> :paste
// Entering paste mode (ctrl-D to finish)
trait Invariant[A]
trait Covariant[+A]
trait Foo
implicit def invariantTuple1[T1: Invariant]: Invariant[Tuple1[T1]] = ???
implicit def invariantFoo: Invariant[Foo] = ???
def a = implicitly[Invariant[Foo]]
implicit def covariant[A: Invariant]: Covariant[A] = ???
def b = implicitly[Covariant[Foo]]
// Exiting paste mode, now interpreting.
def b = implicitly[Covariant[Foo]]
^
<pastie>:13: invariantTuple1 is not a valid implicit value for Invariant[T1] because:
hasMatchingSymbol reported error: diverging implicit expansion for type Invariant[T1]
starting with method invariantFoo
def b = implicitly[Covariant[Foo]]
^
<pastie>:13: invariantFoo is not a valid implicit value for Invariant[T1] because:
diverging implicit expansion for type Invariant[T1]
starting with method invariantTuple1
def b = implicitly[Covariant[Foo]]
^
<pastie>:13: this.invariantTuple1 is not a valid implicit value for Invariant[T1] because:
hasMatchingSymbol reported error: diverging implicit expansion for type Invariant[T1]
starting with method invariantFoo
def b = implicitly[Covariant[Foo]]
^
<pastie>:13: covariant is not a valid implicit value for Covariant[Foo] because:
hasMatchingSymbol reported error: ambiguous implicit values:
both method invariantFoo of type => Invariant[Foo]
and method invariantTuple1 of type [T1](implicit evidence$1: Invariant[T1])Invariant[(T1,)]
match expected type Invariant[A]
def b = implicitly[Covariant[Foo]]
^
<pastie>:13: this.covariant is not a valid implicit value for Covariant[Foo] because:
hasMatchingSymbol reported error: ambiguous implicit values:
both method invariantFoo of type => Invariant[Foo]
and method invariantTuple1 of type [T1](implicit evidence$1: Invariant[T1])Invariant[(T1,)]
match expected type Invariant[A]
def b = implicitly[Covariant[Foo]]
^
<pastie>:13: error: could not find implicit value for parameter e: Covariant[Foo]
I have asked about the first two messages as a separate question here Why does scalac seem to expand implicits too far? but I have included the output as it may be relevant to this question.
What I want to know for this question is specifically why does it say:
covariant is not a valid implicit value for Covariant[Foo] because:
hasMatchingSymbol reported error: ambiguous implicit values:
both method invariantFoo of type => Invariant[Foo]
and method invariantTuple1 of type [T1](implicit evidence$1: Invariant[T1])Invariant[(T1,)]
match expected type Invariant[A]
Why does invariantTuple1
match the expected type Invariant[A]
? Doesn't it know that A
is Foo
and so that is not a valid match?
来源:https://stackoverflow.com/questions/62335096/why-do-both-of-these-implicits-match-when-only-one-of-them-would-type-check