1

I have a custom annotation declared as

@Target(AnnotationTarget.FUNCTION)
annotation class Anno(val value: IntArray, val attr2: Int = 0)

For a single element declaration, I'm able to use the above annotation in a Java class as

@Anno(1)

but while writing the same in a Kotlin class, I have to put enclosing brackets

@Anno([1])

Aren't the brackets unnecessary in this case or am I declaring the annotation wrong? I am using Kotlin version 1.2.0-rc-39

1 Answer 1

1

Yes, square brackets (Kotlin 1.2+) or arrayOf (Kotlin 1.2-) are required.

But as long as this is your annotation, written in Kotlin, you can do fancy things with it, like taking lambdas or varargs, so you can try to tune the resulting syntax for your need. For example, this will be valid syntax, even in Kotlin 1.2-:

@Target(AnnotationTarget.FUNCTION)
annotation class Anno(
    val attribute: String,
    vararg val value: Int
)

@Anno("test", 1, 2, 3)
fun test() = 42

You'll need to put vararg parameter at the end.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was missing the vararg modifier. This works as expected

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.