Skip to content

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

Closed

Description

@scabug

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>

Activity

  1. scabug commented on Mar 17, 2014

    @scabug
    Author

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

  2. scabug commented on Mar 18, 2014

    @scabug
    Author

    @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)

  3. scabug commented on Mar 18, 2014

    @scabug
    Author

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

  4. scabug commented on Mar 18, 2014

    @scabug
    Author

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

  5. scabug commented on Mar 18, 2014

    @scabug
    Author

    @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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions