https://kotlinlang.org logo
Title
p

Peter Kehl

06/09/2019, 12:44 AM
Similar with
class Foo { lateinit var i: Integer }
var foo= Foo()

foo.i.isInitialized // =>
// error: unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
// @SinceKotlin @InlineOnly public val @receiver:AccessibleLateinitPropertyLiteral KProperty0<*>.isInitialized: Boolean defined in kotlin

foo::i.isInitialized // ->error: backing field of 'var i: Integer' is not accessible at this point
Help, please.
s

Shawn

06/09/2019, 3:21 AM
I can’t be 100% sure but I think backing field access is restricted to within the class or file
class Foo {
  lateinit var i: String

  fun bar() {
    if (::i.isInitialized) {

    }
  }
}

lateinit var j: String

fun qaz() {
  if (::j.isInitialized) {

  }
}
both are valid ways of using
isInitialized
ancillary to your initial question, you shouldn’t be using
Integer
unless strictly necessary - use
Int
instead, and try to just set a default value
😑 1
p

Peter Kehl

06/09/2019, 3:39 AM
Thank you Shawn.