[
https://issues.apache.org/jira/browse/GROOVY-10000?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17735288#comment-17735288
]
Eric Milles edited comment on GROOVY-10000 at 6/20/23 1:29 PM:
---------------------------------------------------------------
JLS states that interface constants (aka fields) must be initialized by a
constant value (link below). I think that is what limits the possible types to
primitives, wrappers, and strings. It seems reasonable to support "public
static final String EMPTY = '';" in a trait and render it in the interface
bytecode. No static method initialization or static initializers would be
supported. I don't think an annotation is required to indicate this, although
you could make your own as an alias for "public static final" modifiers.
[~blackdrag] [~cchampeau] Do you forsee any major blockers to changing trait
field handling to support interface constants? Do you think it would be
confusing if "public static foo" gets written into the implementing class and
"public static final bar" gets written into the trait?
https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.3
was (Author: emilles):
JLS states that interface constants (aka fields) must be initialized by a
constant value (link below). I think that is what limits the possible types to
primitives, wrappers, and strings. It seems reasonable to support "public
static final String EMPTY = '';" in a trait and render it in the interface
bytecode. No static method initialization or static initializers would be
supported. I don't think an annotation is required to indicate this, although
you could make your own as an alias for "public static final" modifiers.
[~blackdrag] [~cchampeau] Do you forsee any major blockers to changing trait
field handling to support interface constants?
https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.3
> 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
> Reporter: Christopher Smith
> Priority: Major
> Labels: trait, traits
>
> 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}
> trait MyTrait {
> public static final String PREFIX = 'foo'
> }
> {code}
> {code:groovy}
> // in another class
> 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.20.10#820010)