Search code examples
scalaclosures

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.


Solution

  • 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.