Auto-exclude delegated properties?
edenman opened this issue ยท 8 comments
I've got a data class like this:
@PaperParcel
data class MyParcelable(val taco: String, val burrito: String) : PaperParcelable {
companion object {
@JvmField val CREATOR = PaperParcelMyParcelable.CREATOR
}
val calculatedProperty by lazy { taco + burrito }
}
which gives me:
e: /myproject/build/tmp/kapt3/stubs/release/domain/MyParcelable.java:6: error: All field type arguments must be specified.
e:
e: private final kotlin.Lazy calculatedProperty$delegate = null;
I know I can add my own exclude annotation and apply it to the field, but that sort of seems like overkill for a fairly normal usecase. Thoughts?
The transient keyword still works for excluding fields. In kotlin it's an annotation, e.g. @field:Transient
The error message here is not that helpful though. I guess kotlin.Lazy
is a generic class but they neglect generics in the kapt Java stubs. Not sure what to do about that
ahhh, i didn't know about the @delegate:Transient
syntax, that works for me.
As for auto-excluding these, I wonder if we could auto-exclude properties that end with $delegate
or something (although that's an implementation detail so it might be ill advised). Hopefully all delegates inherit from some interface or base class, that'd make it easy. I'll leave this open for now and look into what the options are later.
Yeah, I'd be in favor of that. Although it might be a surprising change if there's anybody out there relying on the current behavior to save computation even over a parcel/unparcel cycle.
@edenman @grandstaish There are 2 types of Delegates ReadOnlyProperty
and ReadWriteProperty
, the later one, may be a valid field to be saved. One use-case for a ReadWriteProperty
is an Observable Field, no computations performed, just trigger some event.
So I believe excluding all delegates can be dangerous. At least a really nice warning should be raised.
I can't apply annotations to lazy properties, I get this error:
'@field: annotations could only be applied to properties with backing fields
. Is there a workaround?
Does @delegate:Transient
work?
Yup, worked perfectly ๐