Issue: Scala class singleton with non-default constructor parameters


#1

Hi community,

Migrating the discussion from https://github.com/scala/bug/issues/10537 per @SethTisue request.

I have already read about issues with objects inheriting from companion classes.

For example:

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!


#2

What happens when you try the call-by-name scenario? Is there an error? If so, what is it?


#3

They all yield the same error as the first snippet (with the caret pointing at the T in TEST_NAME)


#4

I have created a code snippet: https://scastie.scala-lang.org/ZuvXne8nQaWZc8EUWMclGg


#5

Thanks. I tried changing the constructor param to by-name and indeed got the same error message. At this point this can be considered a bug.