Guanyun
04/06/2023, 8:04 AMwhen
for conditional judgment:
when {
a.isCaseA -> handleCaseA()
a.isCaseB -> handleCaseB()
a.isCaseC -> handleCaseC()
else -> Unit
}
I'd like to refactor these lines with
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 colorephemient
04/06/2023, 11:13 AMephemient
04/06/2023, 11:14 AMGuanyun
04/07/2023, 12:51 AMStephan Schröder
04/08/2023, 10:00 PM