kotlin-style-guide icon indicating copy to clipboard operation
kotlin-style-guide copied to clipboard

Annotation formatting

Open yole opened this issue 9 years ago • 4 comments
trafficstars

Annotations are typically placed on separate lines, before the declaration to which they are attached, and with the same indentation:

@Target(AnnotationTarget.PROPERTY)
annotation class JsonExclude

Annotations without arguments can be placed on the same line:

@JsonExclude @JvmField
var x: String

A single annotation without arguments should be placed on the same line as the corresponding declaration:

@Test fun foo() { ... }

File annotations are placed after the file comment (if any), before the package statement, and are separated from package with a blank line (to emphasize the fact that they target the file and not the package).

/** License, copyright and whatever */
@file:JvmName("FooBar")

package foo.bar

yole avatar May 31 '16 17:05 yole

Can the guide recommend placing short annotations on the same line? Like @Test and others. A short annotation is something not much longer than override, e.i. less than 10 characters.

voddan avatar Jun 09 '16 15:06 voddan

@voddan changed 'can' to 'should'

yole avatar Jun 12 '16 09:06 yole

What about mixing annotations with and without arguments?

@Name("Y")
@JsonExclude @JvmField
var x: String

stepango avatar Aug 31 '16 03:08 stepango

@stepango Your example looks fine to me.

yole avatar Aug 31 '16 08:08 yole