Reputation: 5497
I'm trying to use Kotlin's new feature of checking lateinit property status, but got this compile time error Unresolved reference: isInitialized
I have configure my build.gradle file with kotlin version of kotlin_version = '1.2.0-beta-31'
(android studio version is 3.0)
and also updated kotlin plugin with same version.
this is my code snippet, where I'm using isInitialized
check.
also had a reflect library included
compile group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: '1.2.0-beta-31'
.
lateinit var k: SomeObjectType
fun instance(): SomeObjectType {
if (::k.isInitialized) {
k = SomeObjectType()
}
return k
}
Upvotes: 7
Views: 5288
Reputation: 2558
isInitialized is not working for local lateinit var but it is working fine for global ones.
Upvotes: 1
Reputation: 3269
Project_level.gradle
buildscript {
ext.kotlin_version = '1.2.30'
...
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
...
}
}
App_level.gradle
dependencies {
...
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
...
}
Usage
class Foo() {
lateinit var theVariable: String
// this is required to access isInitialized
fun foo() {
this::theVariable.isInitialized
}
}
Upvotes: 0
Reputation: 18929
If isInitialized
appears in red, check that you have upgraded your Kotlin version to >1.2
Check both in your Project and Module Gradle files:
In your Project Gradle check the row:
ext.kotlin_version = "1.2.21"
In your Module Gradle check the row:
compile 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.21'
Despite the other comments, in my case it was NOT necessary to add the reflection library:
"org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
Upvotes: 0
Reputation: 16758
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"
Upvotes: 5
Reputation: 16758
Is your code valid Kotlin code? I tried the following and it seemed to work as expected.
data class SomeObjectType(val value: String)
lateinit var k: SomeObjectType
fun instance(): SomeObjectType {
if (::k.isInitialized) {
return k
} else {
return SomeObjectType("k was not initialized")
}
}
fun main(args: Array<String>) {
println("inst = ${instance()}")
k = SomeObjectType("was initialized")
println("inst = ${instance()}")
}
producing :
inst = SomeObjectType(value=k was not initialized)
inst = SomeObjectType(value=was initialized)
When I tried what you have above: (with the added class SomeObjectType()
class SomeObjectType()
lateinit var k: SomeObjectType
fun instance(): SomeObjectType {
if (::k.isInitialized) {
instance = SomeObjectType()
}
return instance
}
it complains about the instance = SomeObjectType()
line and the return instance
line, which I think makes sense to me. Line 5 being the instance = SomeObjectType()
and line 7 being the return instance
line.
Error:(5, 8) Function invocation 'instance()' expected
Error:(5, 8) Variable expected
Error:(7, 11) Function invocation 'instance()' expected
Here are the links to the online examples. working example, broken example
Upvotes: 0