Question: is there a version of when() that binds ...
# announcements
p
Question: is there a version of when() that binds its argument? It’d be handy to be able to do, e.g.,
Copy code
when (someFunctionCall()) { 
    Success -> it.value
    Error -> { ... error handling code ... }
}
Right now I’m just explicitly declaring a new variable in the when () clause:
Copy code
when (val result = someFunctionCall()) { ... }
which works, but feels like cheating. Is this the actual accepted pattern, or is there a better way?
z
it’s just just the accepted pattern, it’s how the language is designed to be used
p
okay! Cool beans, was a dumb question 😆
thanks.
p
thanks!
o
I like to use .let { when(it) { } }
p
I was doing that for a while, but the extra level of indentation started to bug me
but that’s valid too!
n
if you're already doing
.let
then you can use if
and then you end up with two levels of indentation either way
p
yes, fair!
n
so I guess you can choose based on whether
if
or
when
is more natural, but
.let { when(it)
seems unnecessary
p
indeedy
and it looks like
when (val result = …. )
is a blessed pattern, which I’m more than fine with! Just wanted to make sure