11 ms·
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 possib
by jillesvangurp 7y ago
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 7y agoMaybe 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-expression https://kotlinlang.org/docs/reference/control-flow.html#when... 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.