Generic Annotation with Generic Enum Member

I was toying around with creating an annotation intended to allow specification of a default Enum value.

annotation class DefaultEnumValue<T: Enum<T>>(val value: T)

I get a compiler error stating that T is an “invalid type of annotation member.” Enums are definitely valid annotation members. Is type erasure the reason that something like this is not possible?

Seems like constraining T to be of type Enum would force this type to be an enum type.

Is there a way to achieve this with different syntax?

Java has the same constraint: The type of an annotation element must be an enum type (or String or int etc). Enum is the superclass of all enum types but it not an enum type itself. Relaxing this constraint in Kotlin would make the annotation unusable from Java, since it would have an element that Java does not consider legal

1 Like

Makes sense. Thanks!