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
I think the fundamental reason for this not to work is that
lateinit doesn’t change the type.
With this suggestion - which would work - then adding
lateinit would change the type of the underlying object.
i mean adding the question mark to Int also changes its type from primitive to boxed primitive too, right?
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
Anyway, for now you can use
Delegates.notNull() for a very similar effect:
var num by Delegates.notNull<Int>()
// println(num) // IllegalStateException
num = 8
println(num) // 8