ash164
ash164

Reputation: 241

Method overloading in scala gives compilation error ambiguous reference

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
    }
}

Error

Upvotes: 0

Views: 527

Answers (1)

Jarrod Baker
Jarrod Baker

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

Related Questions