How to annotate getter declared in trait?


Hello ,

I need to annotate getter method or return value for property declared in trait.
Is it possible ?

annotation class fancy {}

trait Foo {

  // where is annotation added ?, no compilation error
  var bar1: String

  // this should add annotation to return of getter ? but it’s not there
  var bar2: [fancy] String

  // error: backed field not allowed in trait
  // but I want just to add annotation to get()
  var bar3:  String?
  get() = ?

thanks, Tibor


Hi Tibor,

Here’s the solution:


trait Foo {
  var bar : Int
  [fancy] get   

There’s no way in the current state of JVM to annotate a return type of anything, you can only annotate declarations: methods, parameters, classes etc.
Kotlin allows annotations on types, but currently gives them no special meaning. When any meaning is given to type annotations, it will be purely compile-time until JSR308 comes out.


Thanks for clarification Andrey.