Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method is defined twice

Tags:

scala

Why scala REPL does not allow to have several function with same names inside another function?

  def wrapper(): Unit = {
    def a: Unit = ???
    def a(i: Int): Unit = ???
  }

error: method a is defined twice

conflicting symbols both originated in file '< console>'

like image 311
Ivan Avatar asked Apr 24 '26 21:04

Ivan


1 Answers

Looks like you want to use overloading (OOP feature) inside a method what is not proper OOP primitive for that and it is not looks resonable from OOP point of view (and I agree with compiler). To align this with OOP features, just wrap it in object:

def wrapper(): Unit = {
  object wr {
    def a: Unit = ???
    def a(i: Int): Unit = ???
  }

  wr.a(10)
  wr.a
}
like image 160
Yuriy Avatar answered Apr 26 '26 20:04

Yuriy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!