UNRESOLVED_REFERENCE Unresolved reference: isInitialized

This is a bug as reported here and is released in v1.2-rc-1

Update: Kotlin 1.2 RC appears to be available as '1.2.0-rc-39', so if you update your plugin and use this version, your issue should be resolved.

As a workaround until you install rc-1, prefixing the variable with this:: works as can be shown in this project.

package com.example.john.so2

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

data class SomeObjectType(val value: String)
lateinit var k: SomeObjectType

class MainActivity : AppCompatActivity() {

    lateinit var k: SomeObjectType

    fun instance(): SomeObjectType {

        if (this::k.isInitialized) {
            return k
        } else {
            return SomeObjectType("k was not initialized")
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        println("instance = ${instance()}")
        k = SomeObjectType("k was initialized")
        println("instance = ${instance()}")
    }


}

which yields:

11-03 19:31:14.496 31982-31982/com.example.john.so2 I/System.out: instance = SomeObjectType(value=k was not initialized)
11-03 19:31:14.496 31982-31982/com.example.john.so2 I/System.out: instance = SomeObjectType(value=k was initialized)

BTW, I left my original answer as it highlights the fact that the correct syntax works in "try-online"

Tags:

Android

Kotlin