scala / bug

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

Class constructor parameter with default value causes companion object initializer to fail #5000

Open scabug opened 13 years ago

scabug commented 13 years ago

The following code fails to compile,

class Foo(t: Any)
class Bar(x: String = "bar")
object Bar extends Foo(new Bar())

The error message is: "super constructor cannot be passed a self reference unless parameter is declared by-name". I would expect either the code to compile or a more informative error message.

javap reveals that the zero-parameter constructor for class Bar with default value x = "bar" is actually defined as init$default$1 in the companion object Bar. I assume the problem is that extends Foo(new Bar()) is trying to access Bar.init$default$1 before object Bar has been constructed.

I couldn't find this restriction in the SLS. If the compile error is correct, perhaps the error message could be improved? Thanks.

Possibly related: #4278 Source: http://stackoverflow.com/questions/7448931/extending-a-class-with-many-implementation-that-each-have-main-methods-in-scala

scabug commented 13 years ago

Imported From: https://issues.scala-lang.org/browse/SI-5000?orig=1 Reporter: Kipton Barros (kbarros) Affected Versions: 2.9.1

scabug commented 13 years ago

Michael Schmitz (schmmd) said (edited on Sep 23, 2011 1:09:21 AM UTC): This also causes the same error.

class Foo(t: Any)
class Bar(x: String)
object Bar extends Foo(Bar.param) {
    val param = "bar"
}

error: super constructor cannot be passed a self reference unless parameter is declared by-name

scabug commented 13 years ago

@paulp said: More interesting corners arise when classes and their companions reference one another.

scabug commented 11 years ago

@adriaanm said: Un-assigning to foster work stealing, as announced in https://groups.google.com/forum/?fromgroups=#!topic/scala-internals/o8WG4plpNkw

scabug commented 11 years ago

@adriaanm said: Unassigning and rescheduling to M6 as previous deadline was missed.

scabug commented 10 years ago

@gkossakowski said: The 2.11.2 is out so I'm rescheduling the issue for 2.11.3.

scabug commented 10 years ago

@retronym said: Updating fix-by version to 2.11.5.