Skip to content

Scala class singleton with non-default constructor parameters #10537

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

Closed
aaabramov opened this issue Oct 3, 2017 · 6 comments
Closed

Scala class singleton with non-default constructor parameters #10537

aaabramov opened this issue Oct 3, 2017 · 6 comments

Comments

@aaabramov
Copy link

I decided to leave this question here also (maybe it is some kind of defect). Just because it can be lost in StackOverflow.
Link to question: Scala class singleton with non-default constructor parameters

Question:
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
@SethTisue
Copy link
Member

SethTisue commented Oct 3, 2017

hi, and thanks for the report.

but, this bug tracker isn't the place for things where you think maybe it might be a bug but you're not really sure. those should stay on Stack Overflow or https://users.scala-lang.org. come here once you're 90% sure it's a bug.

some hints for achieving a minimal yet complete bug report here:

  • is it necessary that the object and the class be companions, or can they be unrelated? if the companion relationship isn't necessary for reproducing the issue, remove it
  • searching on GitHub (not JIRA, that's just archival now and is no longer updated) shows that there are a number of other open tickets involving the "super constructor cannot be passed a self reference unless parameter is declared by-name". what is the relationship of your report to those tickets? it it possible it's a duplicate of one of them?
  • is there anything in the Scala Language Specification that would illuminate whether this is a bug or intended behavior...?

@aaabramov
Copy link
Author

Hi @SethTisue! Thank you for pointing me. I am not an experienced Scala community participator.

The most relates thing that I found in similar issues is the following 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

But I have not understood the resolution from any of the tickets.

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!

@SethTisue
Copy link
Member

let's discuss in one of the aforementioned forums

@aaabramov
Copy link
Author

FYI, @SethTisue quote from users.scala-lang.org (@yawaramin13m):

yawaramin13m
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.

@som-snytt
Copy link

I was in the area of that code and took a look. It notices the error when looking for aliases of super ctor args.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants