louiscad
04/21/2017, 1:22 PMwhen
expression to fail if no case matches, in a syntax similar to !!
for non null assertions.
Currently, I have to add a else
case and throw an IllegalArgumentException
or an IllegalStateException
, which adds a line for each when clause, which is quite a lot for 2-3 cases when expressions.
How about a syntax like the following?
val message: String = when!! {
code == 0 -> "Ok"
result.failed -> "Some error ;)"
}
val text: String = when(number)!! {
0 -> "Zero"
1 -> "One"
}