Hi everyone, I'm using `when` for conditional judg...
# getting-started
g
Hi everyone, I'm using
when
for conditional judgment:
Copy code
when {
  a.isCaseA -> handleCaseA()
  a.isCaseB -> handleCaseB()
  a.isCaseC -> handleCaseC()
  else -> Unit
}
I'd like to refactor these lines with
Copy code
when(a) {
  isCaseA -> handleCaseA()
  isCaseB -> handleCaseB()
  isCaseC -> handleCaseC()
  else -> Unit
}
However, it is not possible. I searched online and I know that there are a few ways to do this (assigning a val in
when()
, or wrapping this
when()
with
let
or
with
outside). I just want to use the most straightforward way. So is it possible to directly retrieve the param of
when
using
it
or
this
in the future? Does Kotlin team plan to add this syntactic sugar? Thanks. thank you color
e
g
@ephemient Thanks a lot!
s
If it's possible to make A, B and C the children of a sealed class/interface, then you could drop the else branch. just saying
👍 1