Nikolay Kasyanov
07/04/2019, 3:05 PMkotlin
fun main(args: Array<String>) {
whatever(-2)
whatever(0)
whatever(2)
}
fun whatever(input: Int) {
if (input < 0) {
"negative"
} else if (input > 0) {
"positive"
} else {
"zero"
}.let {
println(it)
}
}
prints:
zero
positive
robstoll
07/04/2019, 3:09 PMif (input < 0) {
"negative"
} else println(
if (input > 0) {
"positive"
} else {
"zero"
}
)
let
:
(if (input < 0) {
"negative"
} else if (input > 0) {
"positive"
} else {
"zero"
}).let {
println(it)
}
if (input < 0) {
"negative"
} else {
(if (input > 0) {
"positive"
} else {
"zero"
}).let {
println(it)
}
}
Nikolay Kasyanov
07/04/2019, 3:11 PMif-else
subexpression?robstoll
07/04/2019, 3:12 PMNikolay Kasyanov
07/04/2019, 3:12 PMpivovarit
07/04/2019, 3:13 PMwhen
when {
input < 0 -> "negative"
input > 0 -> "positive"
else -> "zero"
}
Nikolay Kasyanov
07/04/2019, 3:17 PMlet
behaviour still feels counterintuitive though 😅SiebelsTim
07/04/2019, 4:27 PMlouiscad
07/04/2019, 5:29 PMelse
expression without parentheses is not good for readability IMHOribesg
07/04/2019, 7:35 PMelse if
is not elif
for a reason 🙂