Defining Annotations
Occasionally, you may have the need to create your own custom annotations.
In Kotlin, annotations are simply classes defined using the annotation
keyword:
annotation class MyAnnotation
@MyAnnotation
class Foo
You are welcome to add constructor parameters to your annotation:
annotation class MyAnnotation(val isThisGood: Boolean)
@MyAnnotation(isThisGood = true)
class Foo
However, there are limits on the data types. For Kotlin/JVM development, Kotlin annotations get converted into Java annotations, and those have limits on their data types. You can use:
-
Int
andLong
-
Float
andDouble
Boolean
String
- any enum that you define
- any other annotation
- classes (data type is
KClass
)
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.