fun main() {
var count = 2
do {
val abc = if (count == 0) {null} else {Test(true)}
if (abc == null) {
break
}
count--
} while(abc.isOk && count >=0)
}
data class Test(var isOk: Boolean)
When compile above code,
I got the following error Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Test?
in } while(abc.isOk && count >=0)
But there is already a null check break inside the loop, why cannot infer it inside while condition?
Fundamentally the compiler can’t be sure that abc won’t be null when you read it. (you can add an instruction in between changing abc or there might another thread changing it).
This does not apply in this case. Here you have a local variable instead of a member variable, so another thread can definitely not change abc.
Theoretically the compiler can infer that abc is not null after the if statement, but it could be that it would make compilation so slow that it is not worth implementing.
Updates:
Interesting that, I found the following inferring work. (because abc is a non null type now)
The following code is working well
fun main() {
var count = 2
do {
val abc = if (count == 0) {null} else {Test(true)} ?: break
count--
} while(abc.isOk && count >=0)
}
data class Test(var isOk: Boolean)