Doing some code I wanted the ability to have a when expression to catch when the value was less than zero. What I wanted was something like:
< 0 -> throw IllegalArgumentException(“cannot be negative”)
0 -> 0
> 0 -> compute(x)
else -> doSomethingElse()
Which is not supported, but I think it would be a good addition to the pattern matching expressions.
If x was an Int I could have done:
in Int.MIN_VALUE..-1 ->
But that is much less readable and notice I had to move from 0 to -1 as my boundary. For a double I would have to make it:
!in 0..Double.MAX_VALUE ->
But that actually is not equivalent as that will also include the NaN value which before went to the else case
So I think when expressions should support relational operator expressions.