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
It really seems like a bug. The type String?
is lost somehow upon assigning null
, so you have to tell the compiler explicitely that it should deal with a String?
.
fun main(args: Array){
var ab: String? = "hello"
ab = null
println((ab as String?)?.toUpperCase()) // explicit cast
// ...or
println(ab?.let { it.toUpperCase() }) // use let
}