2012-09-20 51 views
16

最小化的例子如下:为什么不允许重载方法内的方法(例如重载闭包)?

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

斯卡拉2.9.2编译器一直在说我,method giveMeBigDecimal is defined twice
我知道我workaround this,但好奇,为什么这样的限制存在哪有。

+0

可能的重复 - http://stackoverflow.com/questions/7550302/why-method-overloading-does-not-work-inside-another-method – Rogach

+1

@Rogach它主要是问和答案*如何解决*不*为什么* –

回答

5

这是一个Scala的实现细节,不幸的是,它的规范。 Scala使用闭包类型作为变量实现本地方法,并且不允许在同一方法中使用两个具有相同名称的变量。