Reputation: 241
When I compile this code, I get ambiguous reference error for method m1
. Can someone tell me why?
object MyClass {
trait T {
def m1(str: String): Unit = println(str)
def m1: Unit = {
println("m1")
m1("from:m1")
}
}
class C extends T {
override def m1(str: String): Unit = println(str+"1")
}
def main(args: Array[String]): Unit = {
val c = new C()
c.m1
}
}
Upvotes: 0
Views: 527
Reputation: 1220
When you call C.m1
in main
you don't include parentheses. The compiler doesn't know if you are intentionally calling the arity-0 method, or were intending to call the arity-1 method using infix notation, eg c.m1 "hello"
.
Replacing c.m1
with c.m1()
will compile.
Upvotes: 2