Hi all, I was under the impression that Kotlin 1.8...
# android
d
Hi all, I was under the impression that Kotlin 1.8.20, particularly
kotlinx-parcelize
, gained support for `value class`es, but I still get
Type is not directly supported by 'Parcelize'. Annotate the parameter type with '@RawValue' if you want it to be serialized using 'writeValue()'
. Has anybody some info on this?
And no,
@RawValue
is not useful, as this throws the
value class
as is to the
Parcel
, which then responds with
Parcel: unknown type for value [to-string-representation-of-my-value-class]
e
Copy code
@JvmInline
@Parcelize
value class Id(val id: String) : Parcelable

@Parcelize
data class ParcelExample(val id: Id) : Parcelable
works fine for me in 1.8.20
d
OK, I see, I thought the support for value classes meant that one could use value classes verbatim, but apparently they still need to implement
@Parcelable
, which is a little bummer. If you build up strong typing you want
value class
types to not carry platform-specific implementations, so they’re universally usable. We’ve resided to let our
value
classes implement
Serializable
now, just like
java.lang.String
and others actually do.
e
going through Java serialization is kind of a waste (and is not how String is handled, it and other built-in types are special-cased by parcelize)
perhaps you could provide a library of external parcelers for your value class wrappers, https://developer.android.com/kotlin/parcelize#custom_parcelers
d
I know that Parcelize handles simple types and I really wished for it to also introspect value classes that just wrap simple types to use the same method, without having to annotate anything. Custom parcelers are not ideal either, because whenever one uses such a custom type one would have to remember to give it the proper annotation to let the type be parceled properly.