Ivan
Ivan

Reputation: 462

Method is defined twice

Why scala REPL does not allow to have several function with same names inside another function?

  def wrapper(): Unit = {
    def a: Unit = ???
    def a(i: Int): Unit = ???
  }

error: method a is defined twice

conflicting symbols both originated in file '< console>'

Upvotes: 3

Views: 1296

Answers (1)

Yuriy
Yuriy

Reputation: 2769

Looks like you want to use overloading (OOP feature) inside a method what is not proper OOP primitive for that and it is not looks resonable from OOP point of view (and I agree with compiler). To align this with OOP features, just wrap it in object:

def wrapper(): Unit = {
  object wr {
    def a: Unit = ???
    def a(i: Int): Unit = ???
  }

  wr.a(10)
  wr.a
}

Upvotes: 1

Related Questions