Reputation: 17
I've read Why doesn't Kotlin allow to use lateinit with primitive types?.
However, there is a benefit of using the lateinit
, that is, if the error is caused by no initialization, it can be immediately known from the error message. But for primitive types that cannot use lateinit
, such as Int, the user have to assign a value of 0. But if the appropriate value should be a value that is much greater than 0 and must be determined later, and then, the user forgot to init the value, the program produced an error later, is there any way to make the user who read the error message immediately realize that the error is not caused by other reasons?
thanks a lot.
And lateinit var v:Int? = null
is very bad, which makes operations like v--
become very complex.
Upvotes: 0
Views: 210
Reputation: 28362
The answer linked by you explains why it is technically impossible to support lateinit for primitive types. So even if there are benefits of having them, then... well, see above, it is technically impossible.
You can use a property delegate for a very similar effect:
var v by Delegates.notNull<Int>()
Upvotes: 3