I wanted to use "when with capture" in the followi...
# announcements
n
I wanted to use "when with capture" in the following way:
val b = when(val computed = f()) { computed > 0 -> computed + 1 ; else -> computed - 1 }
but this produces a syntax error. Is the only way really to use
val computed = f(); val b = when {  computed > 0 -> computed + 1 ; else -> computed - 1 }
(with leaking of
computed
outside of the
when
scope)?
a
or a
val b = f().let { when { it > 0 -> it + 1; else -> it - 1} }
I guess if computed is an int then
Copy code
val b = when(val computed = f()) {
  in 1..Int.MAX_VALUE -> computed + 1
  else -> computed - 1
}
could also work
n
I like the
let
solution, thanks!