om-nom-nom
om-nom-nom

Reputation: 62835

Why it doesn't allowed to overload methods inside methods (e.g. overloaded closures)?

A minimized example is the following:

object Main extends App { 
  def f = {
    def giveMeBigDecimal(x: String) = BigDecimal(x)
    def giveMeBigDecimal(x: Double) = BigDecimal(x)
    (giveMeBigDecimal("1.0"), giveMeBigDecimal(1.0))
  }
}

Scala 2.9.2 compiler keep saying me that method giveMeBigDecimal is defined twice
I know how can I workaround this, but curious why such limitation exists.

Upvotes: 16

Views: 263

Answers (1)

Konstantin Solomatov
Konstantin Solomatov

Reputation: 10332

It's a Scala's implementation detail which (unfortunately) made its way to the spec. Scala implements local methods as variables with closure type and it isn't allowed to have two variables with the same name in the same method.

Upvotes: 5

Related Questions