[ 
https://issues.apache.org/jira/browse/GROOVY-8859?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17764794#comment-17764794
 ] 

Eric Milles edited comment on GROOVY-8859 at 9/13/23 5:04 PM:
--------------------------------------------------------------

This could go one of two ways:
1. The semantic of traits is that the code of the trait is copied into the 
implementing class and therefore, it gets the private methods and can make use 
of them.  This is already true for private static methods.
2. A trait's private methods are considered an implementation detail that 
should be protected from implementing classes.

At present, private fields are copied to the implementing class (by necessity) 
but they are obscured (somewhat) with the "T__name" scheme.  But they are still 
accessible to the implementing class like any other private member.  
GROOVY-7136 proposes official access to private fields.

[~blackdrag] [~paulk] Thoughts?


was (Author: emilles):
This could go one of two ways:
1. The semantic of traits is that the code of the trait is copied into the 
implementing class and therefore, it gets the private methods and can make use 
of them.  This is already true for private static methods.
2. A traits private methods are considered an implementation detail that should 
be protected from implementing classes.

At present, private fields are copied to the implementing class (by necessity) 
but they are obscured (somewhat) with the "T__name" scheme.  But they are still 
accessible to the implementing class like any other private member.  
GROOVY-7136 proposes official access to private fields.

[~blackdrag] [~paulk] Thoughts?

> 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: Bug
>          Components: Compiler
>            Reporter: Eric Milles
>            Assignee: Eric Milles
>            Priority: Minor
>              Labels: trait, traits
>
> 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
(v8.20.10#820010)

Reply via email to