Hi community,
Migrating the discussion from Scala class singleton with non-default constructor parameters · Issue #10537 · scala/bug · GitHub per @SethTisue request.
I have already read about issues with objects inheriting from companion classes.
For example:
- Class constructor parameter with default value causes companion object initializer to fail
- super constructor cannot be passed a self reference unless parameter is declared by-name
But I have a bit different issue (I am not sure that it is a bug)
I have the following code:
class Scala(name: String)
import Scala._
object Scala extends Scala(TEST_NAME){
val TEST_NAME = "test name"
}
Note, that I am use variable defined in scope of companion object and then pass it to super’s constructor.
I am getting the following compilation error:
Scala.scala:5: error: super constructor cannot be passed a self reference
unless parameter is declared by-name
object Scala extends Scala(TEST_NAME){
^
one error found
Other variants that I have tried:
Call by name:
class Scala(name: => String)
import Scala._
object Scala extends Scala(TEST_NAME){
val TEST_NAME = "test name"
}
Named argument:
class Scala(name: String)
import Scala._
object Scala extends Scala(name = TEST_NAME){
val TEST_NAME = "test name"
}
Both of them:
class Scala(name: => String)
import Scala._
object Scala extends Scala(name = TEST_NAME){
val TEST_NAME = "test name"
}
Some environment details:
- java:
java version "1.8.0_144"
- javac:
javac 1.8.0_144
- scala:
Scala code runner version 2.12.3
- scalac:
Scala compiler version 2.12.3
- OS:
Darwin ***.local 17.0.0 Darwin Kernel Version 17.0.0: Thu Aug 24 21:48:19 PDT 2017; root:xnu-4570.1.46~2/RELEASE_X86_64 x86_64
The most similar issue that has been reported is this comment:
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
My question is: should the error message be changed? It seems that suggestion unless parameter is declared by-name is useless. Correct me if I am wrong.
Thank you!