How to annotate getter declared in trait?


#1

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
  [fancy]
  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?
  [fancy]
  get() = ?
}

thanks, Tibor


#2

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.


#3

Thanks for clarification Andrey.