I am sure this has been asked before, why no lateinit primitive types?
However, I am just confused with the answer that lateinit primitives are technically impossible
From what I know the null value is used as uninitialized markers for object types underlyingly.
Why can’t we do the same with primitives using boxed primitives (Integer instead of int)?
We are able to do Int? but not lateinit var: Int
why?
Yes, but that’s a direct change to the type, while lateinit is “further away” geographically from the type. I agree that it should just be allowed, but I can see why it isn’t a thing yet.
There is a bug for it on Youtrack: https://youtrack.jetbrains.com/issue/KT-15284
Currently (version 1.9.24), instead of lateinit primitive property I have to declare initialized property:
class Example {
lateinit var property1: String
var property2: Boolean = false
lateinit var property3: StringBuilder
var property4: Int = 0
}
- this looks quite ugly near lateinit object properties. Also, I have to declare some random value.
Why don’t you allow lateinit primitive property with similar initialization for backed field, ignoring scenario where property is not initialized to throw exception