Defining Annotations
Create custom ones.
Declaring an Annotation
You define an annotation with the annotation class keywords. The simplest one carries no data and acts as a marker.
annotation class Beta
@Beta
fun newFeature() = "experimental"
fun main() {
println(newFeature())
}Annotation Parameters
Annotations can hold data through constructor parameters. Allowed types are limited: primitives, String, enums, other annotations, KClass, and arrays of those.
annotation class ApiVersion(val value: Int)
@ApiVersion(2)
fun endpoint() = "v2"
fun main() {
println(endpoint())
}All lessons in this course
- Using Annotations
- Defining Annotations
- Reflection Basics
- Practical Reflection