paperparcel icon indicating copy to clipboard operation
paperparcel copied to clipboard

Auto-exclude delegated properties?

Open edenman opened this issue 9 years ago • 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?

edenman avatar Apr 04 '17 21:04 edenman

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

grandstaish avatar Apr 04 '17 21:04 grandstaish

ahhh, i didn't know about the @delegate:Transient syntax, that works for me.

edenman avatar Apr 04 '17 21:04 edenman

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.

grandstaish avatar Apr 04 '17 22:04 grandstaish

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 avatar Apr 04 '17 22:04 edenman

@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.

Hazer avatar May 11 '17 20:05 Hazer

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?

ansman avatar Jun 08 '17 18:06 ansman

Does @delegate:Transient work?

grandstaish avatar Jun 08 '17 22:06 grandstaish

Yup, worked perfectly 👍

ansman avatar Jun 09 '17 09:06 ansman