Annotations: when arrayOf is required

Suppose we have a Java annotation as follows:

@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Hans { String[] value() default {}; } 

In Kotlin, I am allowed to use the annotation as follows:

 @Hans(value = "test") 

Once I changed the property name from 'value' to 'name', he is no longer allowed to use this syntax, instead I need to have arrayOf (..).

 @Hans(name = arrayOf("test")) 

Is this a mistake or a design decision, and if so, what is the reason for this.

Thanks a lot in advance Regards

+7
kotlin
source share
1 answer

No, this is not a mistake. Java specifically considers value annotation and allows you to omit the name of the annotation parameter when using it. Kotlin follows this special method and also allows you to omit the parameter name, allowing you to write @Hans("test") . Supporting this syntax for array parameters requires processing the parameter as vararg , so Kotlin does this and allows to omit arrayOf .

+7
source share

All Articles