Hi,
I’m trying to understand how this compilation error can occur:
class Foo[T](value: T) {
def bar (implicit ev: T =:= String) : String = value.asInstanceOf[String].toUpperCase
def baz(that: Int)(implicit ev: T =:= Int) : Int = value.asInstanceOf[Int] + that
def baz(that: Int)(implicit ev: T =:= Double, di: DummyImplicit): Double = value.asInstanceOf[Double] + that
}
// ---------------------------------------------------------------------------
def main(args: Array[String]): Unit = {
println(new Foo("hello").bar)
println(new Foo(3.14) .baz(1)) // compilation error:
//ambiguous reference to overloaded definition,
//both method baz in class Foo of type (that: Int)(implicit ev: Double =:= Double, di: DummyImplicit): Double
//and method baz in class Foo of type (that: Int)(implicit ev: Double =:= Int): Int
//match argument types (Int) and expected result type Any
}
If I understand the message correctly, it seems to imply that an implicit instance of =:=[Double, Int]
can be found. If so, it contradicts my understanding of what =:=
is supposed to do.
Any help would be appreciated!
PS: using Scala 2.13.4 here