Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isInitialized - Backing field of lateinit var is not accessible at this point

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 } 
like image 740
ElegyD Avatar asked Nov 29 '17 09:11

ElegyD


2 Answers

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     } } 
like image 178
AutonomousApps Avatar answered Sep 21 '22 13:09

AutonomousApps


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.

like image 37
Raghavendra Avatar answered Sep 20 '22 13:09

Raghavendra