Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-8859

traits allow access to private fields and static methods but not instance methods

    XMLWordPrintableJSON

    Details

    • Type: Question
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Compiler
    • Labels:
      None

      Description

      It seems that private in a trait is akin to protected in a class. For example a class that implements a trait may access private fields (through namespace syntax) and properties and call private static methods. And it may use Type.super.method() to disambiguate methods if necessary. Why, however, can a class that implements a trait not call private instance methods? This is not really covered in the language specification.

      trait T {
          private void privit() {
              println 'private'
          }
          public void publik() {
              println 'public'
          }
      }
      
      class C implements T {
          def m() {
              publik()
              privit()
          }
      }
      
      new C().m()
      

      This fails with missing method. But if static modifier is added to privit, it succeeds.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              emilles Eric Milles
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: