开发者

Why method overloading does not work inside another method?

In the class or object body, this works:

def a(s:String) {}
def a(s:Int) {}

B开发者_C百科ut if it is placed inside another method, it does not compile:

def something() {
  def a(s:String) {}
  def a(s:Int) {}
}

Why is it so?


Note that you can achieve the same result by creating an object:

def something() {
  object A {
    def a(s:String) {}
    def a(i: Int) {}
  }
  import A._
  a("asd")
  a(2)
}

In your example, you define local functions. In my example, I'm declaring methods. Static overloading is allowed for objects, classes and traits.

I don't know why it's not allowed for local functions but my guess is that overloading is a possible source of error and is probably not very useful inside a code block (where presumably you can use different names for in that block scope). I assume it's allowed in classes because it's allowed in Java.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜