Annotations - interface as attribute type
Hey, I haven't found any useful resources on this. Maybe I'm using the wrong keywords, so I figured I should ask here:
Why do annotations not allow interfaces as attribute types?
For some more context: I actually want to pass an enum, but use an interface to define a standard method (implementation can vary).
This is just an example:
8 Replies
⌛
This post has been reserved for your question.
Hey @Aze! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
Annotations are fully defined at compile time. Like, what they are is exactly how they're written. Running the program can't change anything about them.
And while an enum is a compile-time constant, a general type like an interface or class, isn't.
Like if it was accepted, then I could do:
and then
Which is completely contradictory with the concept of annotations
Right, I understand the value has to be a constant, but in my example the interface is implemented by an enum, and I know interfaces can also be implemented by normal classes, like in you example.
But since enums can implement interfaces, why disallow it completely?
Because other things can also implement interfaces
When the interface is applied to an enum it would be a constant too, no?
If I drop the interface and implement the method straight into the enum it works
You must limit yourself to what can't be mistyped. That's the entire concept of static typing
I guess that makes sense
kind of sad, now I have to rethink what I had planned
Appreciate the help!
Post Closed
This post has been closed by <@398509167351955456>.