mkUltra
mkUltra

Reputation: 3068

Why Scala compile does't permit in scope method overload?

I have notice, that scala compiler does't permit in scope method overload. Scala version 2.11.12

Doesn't compile:

val result = {
  def loop(a: String): String = ???

  def loop(i: Int): Int = ???

  ???
}

Compile correctly:

def loop(a: String): String = ???

def loop(i: Int): Int = ???

val result = ???

Question:

Upvotes: 0

Views: 89

Answers (1)

Tim
Tim

Reputation: 27356

Overloading happens when you call a method on an object, it does not happen with bare function calls. So you can only define overloaded methods for a class. A block is not a class, so it can't have overloaded methods.

Your "compile correctly" code will only work if it is placed directly inside a class/object.

Upvotes: 1

Related Questions