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

Private field in class cannot be accessed from companion object #8426

Closed
scabug opened this issue Mar 17, 2014 · 5 comments
Closed

Private field in class cannot be accessed from companion object #8426

scabug opened this issue Mar 17, 2014 · 5 comments
Assignees

Comments

@scabug
Copy link

scabug commented Mar 17, 2014

According to the announcement of 2.11.0-RC1, I report an issue I have with code that compiles file under 2.10.3 but does not compile under 2.11.0-RC1.

The issue can be reduced to the following minimal case:

Welcome to Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_51).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :paste
// Entering paste mode (ctrl-D to finish)

object A extends A
class A {
  private var a: Boolean = false 
  def run { A.a = true }
}

// Exiting paste mode, now interpreting.

defined module A
defined class A

scala>

The same code fails to compile with Scala 2.11.0-RC1

Welcome to Scala version 2.11.0-RC1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_51).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :paste
// Entering paste mode (ctrl-D to finish)

object A extends A
class A {
  private var a: Boolean = false 
  def run { A.a = true }
}

// Exiting paste mode, now interpreting.

<console>:10: error: value a is not a member of object A
         def run { A.a = true }
                     ^

scala>
@scabug
Copy link
Author

scabug commented Mar 17, 2014

Imported From: https://issues.scala-lang.org/browse/SI-8426?orig=1
Reporter: Tobias Roeser (lefou)
Affected Versions: 2.11.0-RC1
See #7475

@scabug
Copy link
Author

scabug commented Mar 18, 2014

@retronym said (edited on Mar 18, 2014 12:03:09 AM UTC):
Thanks a lot or reporting.

As weird as this seems, it is actually according to the Spec.

Private fields are not inherited. So you can't select them from a value of the derived type.

You can upcast and select the member, however.

object A extends A
class A {
  private var a: Boolean = false
  def run { (A: A).a = true }
}

This was changed under #7475. We double checked with Martin, and with the behaviour of Javac, which does the same thing. (Java doesn't have companions, but you can emulate the same thing with nested classes)

@scabug scabug closed this as completed Mar 18, 2014
@scabug
Copy link
Author

scabug commented Mar 18, 2014

@adriaanm said:
I'm adding a paragraph to the release notes with the example from #8371, which is the smallest I've seen so far.

@scabug
Copy link
Author

scabug commented Mar 18, 2014

Tobias Roeser (lefou) said:
Thank you! As this was a bug in 2.10.3, will it be fixed in 2.10.4, too?

@scabug
Copy link
Author

scabug commented Mar 18, 2014

@retronym said:
No, the 2.10.x series will stay bug compatible.

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

No branches or pull requests

2 participants