Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
scabug opened this Issue Sep 17, 2011 · 7 comments

Comments

Projects
None yet
1 participant
@scabug
Copy link

commented Sep 17, 2011

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

This comment has been minimized.

Copy link
Author

commented Sep 17, 2011

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

@scabug

This comment has been minimized.

Copy link
Author

commented Sep 23, 2011

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

This comment has been minimized.

Copy link
Author

commented Oct 11, 2011

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

@scabug

This comment has been minimized.

Copy link
Author

commented Mar 15, 2013

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

@scabug

This comment has been minimized.

Copy link
Author

commented Jul 10, 2013

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

@scabug

This comment has been minimized.

Copy link
Author

commented Aug 5, 2014

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

@scabug

This comment has been minimized.

Copy link
Author

commented Nov 4, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.