
Java switch – 4 wrongs don't make a right - wheresvic3
https://blog.joda.org/2019/11/java-switch-4-wrongs-dont-make-right.html
======
The_rationalist
If only they could add the when construct, similar to kotlin. It's really
something they got right and is in some ways more powerful than rust match.

~~~
kod
Kotlin has the extremely dubious behavior of not checking exhuastivity when
it's a statement. And it doesn't have destructuring, not for principled
reasons but because the developers thought it would be too much work.

How is this more powerful than rust match?

~~~
jillesvangurp
Actually if you use sealed classes or enums Kotlin does warn about things
falling through. Unlike Rust, JVM languages have dynamic linking so this is
not possible for e.g. open type hierarchies or other kinds of things where any
library could be adding new variants.

Other useful features it has are that most constructions are also expresions.
So you can assign the outcome of a when and not having a value for one of the
branchesd then becomes a compile error. IMHO when is vastly superior to Java's
switch. Something like that would also be a great addition to e.g. typescript
which inherits limitations from Java's limited syntax.

Kotlin does have destructuring. You can do val (foo,bar) = returnsomePair().
Also works in lambdas when you do a foreach on e.g. map entries.

~~~
kod
Maybe I wasn't clear enough.

Kotlin checks exhaustivity of when only if it is used as an expression, not as
a statement.

[https://kotlinlang.org/docs/reference/control-
flow.html#when...](https://kotlinlang.org/docs/reference/control-
flow.html#when-expression)

Kotlin doesn't allow destructuring in when:

[https://discuss.kotlinlang.org/t/destructuring-in-
when/2391](https://discuss.kotlinlang.org/t/destructuring-in-when/2391)

This is just straight up weak coming from any reasonable ML derived language
with real pattern matching.

