lateinitVar cannot be resolved to use isInitialized from Kotlin 1.2.10

扶醉桌前 提交于 2020-06-16 06:18:12

问题


I want to use this feature

the simplest thing like in the example does not work for me:

lateinit val foo = 1
val bar = foo::lateinitVar.isInitialized()

But I am getting

unresolved reference lateinitVar

I am using Kotlin 1.2.10 via gradle in Android-Studio (also the Kotlin 1.2.10 plugin installed there)


回答1:


There’s no variable with name lateinitVar in your code, thus the error. Look at the example again:

this::lateinitVar.isInitialized

There’s a variable lateinitVar defined in this, which the function is called on. The code snippet in the example can be expanded (little plus sign at the beginning of the listing) and looks as follows:

class Foo {
    lateinit var lateinitVar: String

    fun initializationLogic() {
        println("isInitialized before assignment: " + this::lateinitVar.isInitialized)
        lateinitVar = "value"
        println("isInitialized after assignment: " + this::lateinitVar.isInitialized)    

    }
}

This might make it more clear.

Also, be aware that lateinit can’t be applied to val but only var.



来源:https://stackoverflow.com/questions/47860289/lateinitvar-cannot-be-resolved-to-use-isinitialized-from-kotlin-1-2-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!