ivan.savytskyi
09/17/2019, 6:31 PMInitializers are called iff (if and only if) property is @Transient or optional and was not read (see below).
Is there any way to change this default behaviour if was not read
, make initializer to be called without check if it was seen or not? (Basically have computed properties that only been serialized but never deserialized)ivan.savytskyi
09/17/2019, 6:34 PMSo it is legal in bytecode to reassign a final field any number of times as long as this reassignment is conducted within a constructor call of the class that declares the field
ivan.savytskyi
09/17/2019, 6:35 PM@Serializable
data class TaxLine(
@Transient
val price: String = "",
@ContextualSerialization
val rate: BigDecimal
) {
@SerialName("price")
private val _price: String
init {
_price = price
}
}
Nikky
09/17/2019, 6:39 PMinit
is the correct approach for running code that should always execute
maybe think about why that initializer function always has to run?
one option might be
val price: String
get() = _price
that will just access the backing field and not require copying data, this could also take multiple fields and calculate the correct value, but then maybe via add inline
edit: @Transient
is not even required with a getterivan.savytskyi
09/17/2019, 6:51 PMBy default, only properties which have backing fields will be serialized and restored back.
ivan.savytskyi
09/17/2019, 6:51 PM