I'm trying to check if a lateinit property has been initialized.
In Kotlin 1.2 we now have the isInitialized method for that. It works when I do that in the class where the lateinit property is declared. But when I try to call this from another class I get the following warning:
Backing field of 'lateinit var foo: Bar' is not accessible at this point
My model class (let's say Person) is written in Java
Two other classes (let's say Test1 and Test2) are written in Kotlin
Example:
class Test1 { lateinit var person: Person fun method() { if (::person.isInitialized) { // This works } } } -
class Test2 { lateinit var test1: Test1 fun method() { if (test1::person.isInitialized) { // Error } } } Any chance to get this working?
My current workaround is to make a method in Test1 which returns isInitialized from the person property.
fun isPersonInitialized(): Boolean = ::person.isInitialized //in Test2: if (test1.isPersonInitialized()) { // Works }
isInitializedover nullable?lateinitshow always be initialized before the object being use.Person?. If you abuselateinit, you need an if everytime you access person which make you go back to Java with null checking.