Uploaded image for project: 'Scala Programming Language'
  1. Scala Programming Language
  2. SI-8426

Private field in class cannot be accessed from companion object

    Details

    • Type: Bug
    • Status: CLOSED
    • Priority: Blocker
    • Resolution: Not a Bug
    • Affects Version/s: Scala 2.11.0-RC1
    • Fix Version/s: None
    • Component/s: None
    • Labels:
    • Environment:

      Scala version 2.11.0-RC1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_51), Gentoo Linux

      Description

      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>
      

        Attachments

          Issue Links

            Activity

            Hide
            retronym Jason Zaugg added a comment - - edited

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

            Show
            retronym Jason Zaugg added a comment - - edited 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 SI-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)
            Hide
            moors Adriaan Moors added a comment -

            I'm adding a paragraph to the release notes with the example from SI-8371, which is the smallest I've seen so far.

            Show
            moors Adriaan Moors added a comment - I'm adding a paragraph to the release notes with the example from SI-8371 , which is the smallest I've seen so far.
            Hide
            lefou Tobias Roeser added a comment -

            Thank you! As this was a bug in 2.10.3, will it be fixed in 2.10.4, too?

            Show
            lefou Tobias Roeser added a comment - Thank you! As this was a bug in 2.10.3, will it be fixed in 2.10.4, too?
            Hide
            retronym Jason Zaugg added a comment -

            No, the 2.10.x series will stay bug compatible.

            Show
            retronym Jason Zaugg added a comment - No, the 2.10.x series will stay bug compatible.

              People

              • Assignee:
                retronym Jason Zaugg
                Reporter:
                lefou Tobias Roeser
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: