Kotlin unresolved reference in IntelliJ

后端 未结 20 1490
太阳男子
太阳男子 2020-12-01 08:47

I started off with the tutorial for learning Kotlin in IntelliJ.When I tried running the example i.e

fun main(args: Array) {
 prin         


        
20条回答
  •  孤街浪徒
    2020-12-01 09:12

    I had this issue because the linter was printing that my object was an instance of Foo whereas the compiler couldn't define its type and consider it was an Any object like (It was more complex in my case, the linter show error in below code but the idea is here) :

        class Foo {
            val bar: Int = 0
        }
    
        fun test(): Any {
            return Foo()
        }
    
        val foo = test()
        foo.bar // <-- Linter consider that foo is an Instance of Foo but not the compiler because it's an Any object so it show the error at compilation.
    

    To fix it I had to cast my object :

    val foo = test() as Foo
    

提交回复
热议问题