scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Loosen "only one overload can define defaults" restriction #8161

Open scabug opened 10 years ago

scabug commented 10 years ago

If we encode the arity of the method into the default getter name, we don't have something completely general, but we would eliminate most of the pain.

scabug commented 10 years ago

Imported From: https://issues.scala-lang.org/browse/SI-8161?orig=1 Reporter: @retronym Affected Versions: 2.12.0-RC1

scabug commented 10 years ago

@retronym said: https://github.com/retronym/scala/compare/ticket;8161?expand=1

scabug commented 10 years ago

@nilskp said: You could go further than that and encode the argument types, using 1 char letters.

SethTisue commented 4 years ago

in short, it makes sense to disallow this:

object O1 {
  def foo(y: String = "bar") = ()
  def foo(y: Int = 3) = ()
}

since foo() is ambiguous, but it seems like this ought to be allowed:

object O2 {
  def foo(x: String, y: String = "bar") = ()
  def foo(x: Int, y: Int = 3) = ()
}

but the compiler rejects it with "in object O2, multiple overloaded alternatives of method foo define default arguments"

https://stackoverflow.com/questions/4652095/why-does-the-scala-compiler-disallow-overloaded-methods-with-default-arguments explains what the problem is. In short, in the current encoding the compiler would need to emit two different zero-arity methods named foo$default$2

The SO answers include suggested workarounds.

The restriction exists in Dotty as well, for interoperability with Scala 2.