Currently when you define an annotation, it is able to be applied to _ANY_ type, such as other annotations, methods, classes, enums, modules etc.
However, in some cases, some annotations are created for specific purposes. I propose there to be some way to specify what types an annotation could be applied to. This would add extra safety to the users of the annotations to make sure they are using them correctly, as well as to possibly prevent unintended effects from applying an annotation on an invalid type.
I could see this working either like
@[Targets("CLASS", "METHOD")]
annotation SomeThing; end
or
annotation SomeThing
TARGETS = ["CLASS", "METHOD"]
end
Then if this annotation was applied to a property:
class Foo
@[SomeThing]
property name : String
end
It would raise a compile time error. Something like: Error: Annotation 'SomeThing' is not allowed to be declared on a property 'Foo@name'. You may only use this annotation on: CLASS, METHOD.
In other words copy Java's @Target
Or as an annotation DSL:
annotation SomeThing
target :class, :method
end
Targets would be really nice. Also, this is definitely a separate issue which might already exist, but it would be nice if annotation logic could be put inside of the annotation rather than annotations just being referenced via macro.
Just opened up a forum RFC relating to this.
Most helpful comment
Or as an annotation DSL: