[
https://issues.apache.org/jira/browse/GROOVY-10000?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17735701#comment-17735701
]
Jochen Theodorou commented on GROOVY-10000:
-------------------------------------------
[~emilles] In short I think the constant would be very nice to have in the
interface class of the trait.
Longer version: Given
{code:Java}
trait MyTrait {
public static final String PREFIX = 'foo'
}
@groovy.transform.CompileStatic
class Other {
void foo() {
def x = MyTrait.PREFIX
}
}
{code}
and
{code:Java}
interface MyTrait {
public static final String PREFIX = 'foo'
}
@groovy.transform.CompileStatic
class Other {
void foo() {
def x = MyTrait.PREFIX
}
}
{code}
I notice that these differ only by one being a trait and the other an
interface. The interface variant will compile, the trait variant not. I
understood that [~chrylis] expects both variants to compile. And I actually
totally understand this from the idea that a trait is at least similar to an
interface. And if it would be a method, it would appear in the interface class.
Then I think we should support public final like that as well.... meaning the
public final static should appear in the interface class that belongs to the
trait. But you have a good point with how this is if it is not final. Let's
boil it down to
{code:Java}
class C {int Foo = 1;}
{code}
and
{code:Java}
interface C {int Foo = 1;}
{code}
in Java the first case it is no constant, in the second case it is and the only
difference is the keyword. So I find it fair that "trait" also defines its own
rules. And that could be non-state and constants in the interface everything
else in the trait helpers. Still users may be confused about this... ah well, I
guess it is not worse in Java already
> 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)