https://kotlinlang.org logo
Title
d

dblack

02/27/2017, 5:01 PM
Is there a way to reference the value being checked in a when statement? I could obviously store the value locally, just trying to be succinct.
when (someCalculatedValue()) {
   1 -> doSomething(/*use value here*/)
   else -> log("nothing")
}
m

mg6maciej

02/27/2017, 5:06 PM
dblack:
doSomething(1)
😉
d

dblack

02/27/2017, 5:12 PM
Bleeeeet. Happy Monday 😆 More readable that way.
I want to log in the else statement though (my example was bad)
Basically... hey, you shouldn't have gotten to the else statement, but we'll log the value that got you here anyways.
m

mg6maciej

02/27/2017, 5:14 PM
I suspected that.
Just
val value = call()
then
when (value) {
, but you know that.
It's weird how people try to bend the language instead of using the simplest constructs. Good example is
variable?.let { }
with
else
if value is null.