How to annotate a delegated field?

Hi,

Let’s say we have a serializable data class with a lazy delegate.

data class Person(val firstName : String, val lastName : String) : Serializable {
  val fullName : String by lazy { "$firstName $lastName" }
}

How could one tell the java serialization framework not to serialize the fullName? The @Transient annotation must be placed on the field, but regardless where I tried to add the annotation, it always created compile error.

Try @delegate:Transient per http://kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets

3 Likes

In you specific case you could create a field as

data class Person(val firstName : String, val lastName : String) : Serializable {
  val fullName : String
    get() = $firstName $lastName"
}

So that the field is not allocated but always generated when called.

yes that is clear, it was not a perfect example, in my case the calculation is really more expensive than a string concatenation and that is what I am trying to avoid