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 2021/03/25 00:40:00 UTC

[jira] [Commented] (GROOVY-10000) Traits should be able to hold constants

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

Eric Milles commented on GROOVY-10000:
--------------------------------------

Can you include your trait definition?  Traits can define properties for implementers, so final and public are not implicit like they are for interfaces.

> Traits should be able to hold constants
> ---------------------------------------
>
>                 Key: GROOVY-10000
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10000
>             Project: Groovy
>          Issue Type: Improvement
>          Components: Compiler
>    Affects Versions: 3.0.7
>            Reporter: Christopher Smith
>            Priority: Major
>
> I can define constants on plain old interfaces; since they're implicitly {{public static final}}, just {{String PREFIX = 'asdf_'}} will get the job done.
> However, in a trait, while I can declare a constant and use it within the trait, I can't access that constant from outside the trait. Instead, I get this error (in static mode for better message; dynamic mode generates bytecode demonstrating the same semantic error):
> {code:groovy}
> doStuff(MyTrait.PREFIX, data)
> {code}
> {code}
> [Static type checking] - No such property: PREFIX for class: java.lang.Class <com.example.MyTrait>
> {code}
> The compiler seems to be treating what should be a distinct symbol {{MyTrait.PREFIX}} as a property access on the class {{MyTrait}}. I expected the {{PREFIX}} constant to be copied to the generated interface {{.class}} file and available as it would be on an interface. Instead, it appears that it's being put into the {{$Trait$Helper.class}} file. Perhaps the trait AST needs a special rule to recognize constants and route them suitably?



--
This message was sent by Atlassian Jira
(v8.3.4#803005)