Why doesn’t Kotlin allow a body inside annotations...
# getting-started
o
Why doesn’t Kotlin allow a body inside annotations? In Java the following is valid:
Copy code
@StringDef({ MyAnnotation.A, MyAnnotation.B, MyAnnotation.C})
@Retention(RetentionPolicy.SOURCE)
public @interface MyAnnotation {
    String A = "A";
    String B = "B";
    String C = "C";
}
But in Kotlin you are forced to do it like this:
Copy code
const val A = "A"
const val B = "B"
const val C = "C"

@StringDef(A, B, C)
@Retention(AnnotationRetention.SOURCE)
annotation class MyAnnotation
or either using an
companion object
, but then a class is also required.
Copy code
class MyKotlinAnnotation {
    companion object {
        const val A = "A"
        const val B = "B"
        const val C = "C"
    }
}

@StringDef(MyKotlinAnnotation.Companion.A, MyKotlinAnnotation.Companion.B, MyKotlinAnnotation.Companion.C)
@Retention(AnnotationRetention.SOURCE)
annotation class MyAnnotation
Or is there another way doing this similar as in Java (by having the constants inside the annotation)?