Why there is ambiguous reference to overloaded definition in this code?
class A {
def m(a1: A, o2: Any): A = {
print("1")
a1
}
def m(a1: A, a2: A): A = {
print("2")
a1
}
def m(o1: Any, o2: Any): Any = {
print("3")
o1
}
def m(o: Any): Any = {
print("4")
o
}
}
class B extends A {
def m(b1: B, o2: Any): A = {
print("5")
b1
}
override def m(a: A, o2: Any): B = {
print("6")
this
}
override def m(o1: Any, o2: Any): Any = {
print("7")
o1
}
def m(i: Int): Unit = { print("8") }
}
val a = new A
val b = new B
b.m(a, a)
The code above gives compile error:
ambiguous reference to overloaded definition,
[error] both method m in class B of type (a: A, o2: Any)B
[error] and method m in class A of type (a1: A, a2: A)A [error] match argument types (A, B)
But my understanding is that method m in class A of type (a1: A, a2: A)A
is more specific
than method m in class B of type (a: A, o2: Any)B
Any hints are appreciated.