I want to draw attention to an idea of chained `wh...
# language-proposals
a
I want to draw attention to an idea of chained `when`’s by @Wout Werkman:
Copy code
val m = list.min().when { NoSuchElement -> 0 }

someExpression.when { 
  it < 0   -> Int.MIN_VALUE
  it > 100 -> Int.MAX_VALUE
}.doOtherStuff()
Default else branch is obviously
else -> it
. We propose only allowing the anonymous parameter `it`; if you need to name it, use the ordinary when. Chained `when`’s seem to be the perfect fit for rich errors where the elvis operator
:?
is no longer, but have great applications otherwise.
j
I'm split on this.
when
looks too much like an extension function with a lambda and it's hard to distinguish. On the other hand, this is really clean. Come to think of it,
when
as it stands bugs me a bit for the same reason.
if
and the other controls don't have the same problem because they already act really closely to a function with a lambda input, but
when
magically treats its curly braces differently from both class declarations AND function bodies.
j