Greetings!
Help me please undestand why this piece of code does not do what I expect it to do.
Code snippet
class SandboxKtTest {
@Test
fun test() {
val a = 4
val name = if (a < 5) {
listOf(3,5,7)
} else if (a >5) {
listOf(1,2,3)
} else {
emptyList()
}.filter {
it == 3
}
println(name)
}
}
Expectations:
the result is [3]
Actual:
the result is [3,5,7].
Observations:
if i change a
to 5 and 6 correspondingly it will apply .filter
. But not for the first branch. why?
reviewing bytecode confirms this.
Kotlin 2.1.0