I'm getting Overload resolution ambiguity error on kotlin safe call

后端 未结 5 2174
眼角桃花
眼角桃花 2021-02-20 01:27

I have a nullable string variable ab. If I call toUpperCase via safe call operator after I assign null to it, kotlin gives error.

fun m         


        
5条回答
  •  长情又很酷
    2021-02-20 02:06

    I'm not sure but that seems to be a bug due to smart casting (to Nothing?, subtype of every nullable type). This one works:

    fun main(args: Array) {
        var ab: String? = "hello"
        ab = makeNull()
        println(ab?.toUpperCase())
    }
    
    fun makeNull(): String? = null
    

    The only difference: The compiler does not know the null assignment directly, which seems to cause the error in your example. But still, yours should probably work too.

提交回复
热议问题