Hello, Christophe. Great post!
One small fear about this snippet of code
inline fun <T : Enum<T>> Parcel.writeEnum(value: T?) =
writeInt(value?.ordinal ?: -1)
You suggest to use ordinal
but as far as I know, it indicates a position in enum declaration. So there are at least one case (rare, but nevertheless), when your solution can lead to bugs with using ordinal
.
Suppose you using your model as KParcelable
to save data in memory. You (as user) save it and close application. Then some programmer adds new features and logic and touches aforementioned enum
. It adds new item, but not in the end, but suppose in the beginning of enum
class. Then user updates application.
As you probably already guessed, it will restore not saved enum
value, but it’s neighbor, since order of each item has changed. Also, there are two links for caution of ordinal
usage. Doc and stackoverflow.
I agreed, that is you will be using names of enums or some id-fields of them, you will still able to get such bug, but the code in that case will be less error prone, than in case of using ordinal
.