In Kotlin, one can attach metadata to declarations in the form of annotations. To declare an annotation, put the annotation annotation (no pun intended ) in front of a normal class:
(Note that, by convention, annotation classes are named with the lowercase fitst letter; the reason will be clear from the examples below.)
Now we can annotate a declaration or an expression with the new fancy annotation. In general, one puts square brackets around the annotation name:
|Annotations are under development|
When annotating a declaration (e.g. a function or a class), the square brackets may be omitted:
Note that square brackets are required for expressions.
Annotation classes may have constructors that take parameters. For example:
As you can see, to pass arguments to an annotation one simply calls its constructor.
On the JVM one can re-use Java annotations:
If you want your Java annotations to look like modifiers, you can rename them on import: