samanime
samanime

Reputation: 26527

Kotlin Pass Annotation as parameter to Annotation

In Kotlin, I am trying to create an Annotation which can accept an array (vararg) of other annotations.

I'm trying to do something like this:

annotation class MyAnnotation(
  vararg val annotation: Annotation
)

The problem is it tells me Annotation is not a valid parameter for annotation. If I try a specific annotation class, it works.

I'm trying to create an annotation which can take other annotations (for validations) and process them in order and do one unified effect.

Is there some special Kotlin syntax I can use to get it to let me provide an array of various annotation?

Upvotes: 3

Views: 1702

Answers (1)

Lucas Ross
Lucas Ross

Reputation: 1089

I believe anything that is nullable (interfaces, Java types...) cannot be used as parameter to a Kotlin annotation. Maybe you could use

annotation class MyAnnotation(vararg val annotations: KClass<in Annotation>)

Upvotes: 1

Related Questions