isInitialized - Backing field of lateinit var is not accessible at this point
According to the docs:
This check is only available for the properties that are lexically accessible, i.e. declared in the same type or in one of the outer types, or at top level in the same file.
Which is why you cannot check that in the main function.
A really simple workaround to the constraints described by the accepted answer is the following:
class LateClass {
lateinit var thing: Thing
fun isThingInitialized() = ::thing.isInitialized
}
class Client {
val lateClass = LateClass()
... things happen ...
if (lateClass.isThingInitialized() {
// do stuff with lateClass.thing, safely
}
}
My version as a Kotlin property.
class LateClass {
lateinit var thing: Thing
val isThingInitialized get() = this::thing.isInitialized
}