https://kotlinlang.org logo
#announcements
Title
# announcements
s

Shawn

10/09/2017, 3:24 PM
Nobody is arguing against using early returns at all. What we are a bit concerned about is the fact that this code snippet is pretty hard to interpret, and that in of itself may warrant rewriting this block to be more easily understood. That said, sure, in a vacuum “style” does not solve your problem, but it does make it harder to give you advice. Based on your initial snippet, I would suggest something that looks more like this:
Copy code
when (i) {
  in 0..10 -> println("matched")
  else -> println("not matched")
}
println("finished")
n

nil2l

10/09/2017, 3:26 PM
Nice, but where are returns and labels?
s

Shawn

10/09/2017, 3:27 PM
if I understand what your needs are correctly, you shouldn’t need a return or label. If
i
is in
0..10
, it will print
matched
, and then exit the
when
block. If
i
is not in that range, you’ll hit the
else
case instead. They will never both be executed
n

nil2l

10/09/2017, 3:29 PM
Sorry, but no. Code I wrote is just synthetic sample of code which means nothing.