You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (JIRA)" <ji...@apache.org> on 2018/10/27 11:30:00 UTC

[jira] [Created] (GROOVY-8859) traits allow access to private fields and static methods but not instance methods

Eric Milles created GROOVY-8859:
-----------------------------------

             Summary: traits allow access to private fields and static methods but not instance methods
                 Key: GROOVY-8859
                 URL: https://issues.apache.org/jira/browse/GROOVY-8859
             Project: Groovy
          Issue Type: Question
          Components: Compiler
            Reporter: Eric Milles


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.

{code:groovy}
trait T {
    private void privit() {
        println 'private'
    }
    public void publik() {
        println 'public'
    }
}

class C implements T {
    def m() {
        publik()
        privit()
    }
}

new C().m()
{code}

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



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)