poohbar
12/16/2018, 3:26 PMlist.map {
if (condition) {
42
}
0
}
I am used to not doing else branches in Java when not needed but it seems like here I get a list of zeros if I don't put the 0 into an else branch. 🤔 Has this bitten anyone before?blogscot
12/16/2018, 3:35 PMDominaezzz
12/16/2018, 3:36 PMpoohbar
12/16/2018, 3:38 PMblogscot
12/16/2018, 3:38 PMfun main() {
val list = listOf(1,2,3,4,5)
val result = list.map {
if (it and 1 == 0) {
42
} else {
0
}
}
println(result)
}
Now it returns [0, 42, 0, 42, 0]
poohbar
12/16/2018, 3:39 PMblogscot
12/16/2018, 3:43 PMbjonnh
12/16/2018, 4:18 PMJukka Siivonen
12/16/2018, 7:00 PMAngusMorton
12/17/2018, 1:09 AMreturn@map
(https://kotlinlang.org/docs/reference/returns.html#return-at-labels)bjonnh
12/17/2018, 3:52 AMHauke Radtki
12/17/2018, 9:12 AM