This is quite important if you deal with functions with side effects (which you should avoid as much as possible anyway)
Copy code
@Test
fun testShortCircuit() {
var a = 0;
fun incA(): Boolean {
a++;
return true
}
if (a == 0 || incA()) {
// is a 0 or 1?
println(a)
}
}
compared to
Copy code
@Test
fun testShortCircuit() {
var a = 0;
fun incA(): Boolean {
a++;
return true
}
if (incA() || a == 0) {
// is a 0 or 1?
println(a)
}
}
a
Alowaniak
12/17/2019, 2:22 PM
I think the
Safe call short-circuits
is a bit misleading?
Because
null?.let{}.let{println("hi")}
still prints hi (which I think it ideally wouldn't because then you wouldn't have to chain null-safe operator for non-nullable properties)
But I guess it makes sense in the sense that the direct right-hand part isn't evaluated