Stylianos Gakis
01/25/2023, 9:33 AMval x = 0
val result1 = if (x == 0) {
10
} else if (x < 5) {
5
} else {
7
}.coerceAtMost(6)
println("$result1 <- should be 6 not 10?")
It actually prints 10, not 6, as the coerceAtMost(6)
somehow only applies to the last 2 branches in the if-else chain but not the first one?
More examples here https://pl.kotl.in/COmzDaqZB with various number of if-else branches where it seems to work only for the last 2 branches, this is like suuuuuper odd, what am I missing here? 🥴
My assumption here was that the result of the entire if expression is evaluated and then passed inside coerceAtMost()
. Or even if that were not the case, I would expect it to only count the last else
case, but not the one above it too.Sam
01/25/2023, 9:34 AMJoffrey
01/25/2023, 9:36 AMif
and a nested one in the else
. But honestly I never had a problem with that for 2 stylistic reasons.
1. Those chains are nicer to read as when
expressions, which are single expressions
2. Calling a method at the end of a block like this really doesn't look good to me, and I would certainly extract either a variable or a method for thisMichael de Kaste
01/25/2023, 9:36 AMval result = when{
x == 0 -> 10
x < 5 -> 5
else -> 7
}.coerceAtMost(6)
Stylianos Gakis
01/25/2023, 9:38 AM/*} else */ if (num < 0) { // ignores everything before the if here
"negative"
} else {
"zero"
}
and applies it there.
This does make a lot of sense then with this knowledge, but I can’t imagine anyone intuitively expecting this to be the way it works.
Will go with a when expression here, or simply save the result of the if expression in a variable and apply coerceAtMost to that one, but yeah this is crazy. This could almost be the first time I’ve seen such an unexpected behavior from Kotlin where I wasn’t expecting it at all.
Thanks everyone for the quick responses. People certainly seem to have opinions on this topic, 3 responses within 2 minutes is quite unusual 😂 But I upvoted the issue, at least an IDE warning should be warranted there, suggesting switching to a when expression instead.Landry Norris
01/25/2023, 3:14 PMif(condition1) {
...
} else {
if(condition2) {
...
} else {
if(condition3) {
...
} else {
...
}
}
}
if that helps to understand it more visually.Stylianos Gakis
01/25/2023, 3:18 PM