Say I have the following class:
class ExampleClass: OtherClass {
lateinit var thing: Thing
var thingOptional: Thing? = null
fun exampleFun() {
val innerThing = thing ?: Thing()
val innerThing2 = thingOptional ?: Thing()
}
}
Produces the following warning:
Elvis operator (?:) always returns the left operand of non-nullable type Thing
and then the following runtime exception:
lateinit property thing has not been initialized
So in my understanding an optional can be either a type, or null at any point, while a lateinit
variable can be null until it is given a value and then can never be null again, and must be given a value. However quite clearly the semantics of lateinit
are slightly different.
So: what is the starting value of lateinit
why isn't it falsy? Is it possible to use lateinit
in this way, as an optional substitute? If not, how can I create this kind of value? (allow null as a starting state, but then be non nullable)