Mark
05/30/2022, 8:40 AMvar check1 = true
var check2 = false
val test = if (check1) {
null
} else if (check2) {
listOf("foo")
} else {
listOf("bar")
}.map { 2 }
println(test)
It seems we don’t need to use safe null on the map
call. However, if we remove the middle branch, then we do need it:
var check1 = true
var check2 = false
val test = if (check1) {
null
} else {
listOf("bar")
}?.map { 2 }
println(test)
In both cases (without the map
), the type of test
is List<String>?
sanggggg
05/30/2022, 8:43 AMfun main() {
var check1 = true
var check2 = false
val test = if (check1) {
null
} else {
if (check2) {
listOf("foo")
} else {
listOf("bar")
}.map { 2 }
}
println(test)
}
Mark
05/30/2022, 8:44 AMephemient
05/30/2022, 8:44 AMephemient
05/30/2022, 8:45 AMMark
05/30/2022, 8:56 AMephemient
05/30/2022, 9:01 AMwhen {
check1 -> 1
check2 -> 2
else -> 3
}.let { println("got $it") }
Mark
05/30/2022, 9:19 AMwhen
instead of else if
does sound like the more sensible approachsanggggg
06/01/2022, 7:40 AMephemient
06/01/2022, 8:24 AMMark
06/01/2022, 1:20 PM