Why can't I initialize a "@Transient" property in ...
# getting-started
v
Why can't I initialize a "@Transient" property in init block?
Copy code
Serializable
data class Item(
    val count: Int,
    val price: Int
) {
   @Transient
   val totalPrice = count*price //this works
   @Transient
   val totalPrice2 //This property is marked as @Transient and therefore must have an initializing expression

   init {
      totalPrice2 = count*price //this does not
   }
}
a
because it's a Val. Vals are immutable and have to be set where they're declared. If you want to do it in the init block, you could make it a var (though then you lose the immutablility, which, to me anyway, is less than ideal)
v
If the property is not marked as @Transient I can init val inside init block
a
corrected my msg but lateinit would also require the var I think, need more coffee
c
It does.
v
There is inconsistency. If i have a val property that is not transient, but initialize it with default value, It behaves like transient anyway. But if i move the initialization to the init block, then it's no longer transient. The question is what the difference? If i initialize someting in the init block, it's the same as default value, it's still a val. So why it doesn't work
m
I do wonder if this is something special due to data class or Serializable class or both. I know in general (on JVM) if you initialize in the constructor things have the default value in the parent class init block.