我试图使用Kotlin检查lateinit属性状态的新功能,但得到了这个编译时错误Unresolved reference: isInitialized
我使用kotlin版本kotlin_version = '1.2.0-beta-31'
配置我的build.gradle文件(android studio版本为3.0)
并且还更新了相同版本的kotlin插件。
这是我的代码段,我使用isInitialized
检查。
还包含一个反射库
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
}
答案 0 :(得分:3)
更新:Kotlin 1.2 RC似乎可用作' 1.2.0-rc-39',所以如果你更新你的插件并使用这个版本,你的问题应该是解决。
作为解决方法,在安装rc-1之前,使用this::
为变量添加前缀可以在此项目中显示。
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()}")
}
}
产生:
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)
顺便说一句,我离开了原来的答案,因为它突出了正确的语法在"尝试在线"
答案 1 :(得分:1)
isInitialized不适用于本地Lateinit变量,但适用于全局变量。
答案 2 :(得分:0)
您的代码是否有效Kotlin代码? 我尝试了以下内容,似乎按预期工作。
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()}")
}
生产:
inst = SomeObjectType(value=k was not initialized)
inst = SomeObjectType(value=was initialized)
当我尝试上面的内容时:(添加了class SomeObjectType()
class SomeObjectType()
lateinit var k: SomeObjectType
fun instance(): SomeObjectType {
if (::k.isInitialized) {
instance = SomeObjectType()
}
return instance
}
它抱怨instance = SomeObjectType()
行和return instance
行,我觉得这对我有意义。第5行是instance = SomeObjectType()
,第7行是return instance
行。
Error:(5, 8) Function invocation 'instance()' expected
Error:(5, 8) Variable expected
Error:(7, 11) Function invocation 'instance()' expected
以下是在线示例的链接。 working example,broken example
答案 3 :(得分:0)
如果isInitialized
显示为红色,请检查您是否已将Kotlin版本升级为&gt; 1.2
检查Project和Module Gradle文件:
在Project Gradle中,检查行:
ext.kotlin_version =&#34; 1.2.21&#34;
在您的Module Gradle中,检查行:
编译&#39; org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.21&#39;
尽管有其他评论,但就我而言,没有必要添加反射库:
"org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
答案 4 :(得分:0)
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"
...
}
用法
class Foo() {
lateinit var theVariable: String
// this is required to access isInitialized
fun foo() {
this::theVariable.isInitialized
}
}