Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

274
Vistas
Check if a local lateinit variable is initialized

Member lateinit variables initialization can be checked with:

class MyClass {
    lateinit var foo: Any
    ...
    fun doSomething() {
        if (::foo.isInitialized) {
           // Use foo
        }
    }
}

However this syntax doesn't work for local lateinit variables. Lint reports the error: "References to variables aren't supported yet". There should logically be a way to do that since lateinit variables are null internally when uninitialized.

Is there a way to check if local variables are initialized?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The code you show in your question is actually fine in Kotlin 1.2 and beyond, since foo is an instance variable, not a local variable. The error message you report and mentioned in Alexey's comment (Unsupported [References to variables aren't supported yet]) can be triggered by a true local variable, for example in the doSomethingElse method below.

class MyClass {
    lateinit var foo: Any

    fun doSomething() {
        if (::foo.isInitialized) {  // this is fine to use in Kotlin 1.2+
           // Use foo
        }
    }
    fun doSomethingElse() {
        lateinit var bar: Any

        if (::bar.isInitialized) {  // this is currently unsupported (see link in Alexey's comment.
            // Use bar 
        }

    }

}

So it seems like this is currently unsupported. The only place that comes to mind where a lateinit local would be used would be if the local is variable captured in a lambda?

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda