4 ms·
But then you should compare a trait with no default method to a switch with no default case. And a switch with no default case would be reported as an error if
by pkolaczk 6y ago
But then you should compare a trait with no default method to a switch with no default case. And a switch with no default case would be reported as an error if some cases are not covered.
Also a switch with no default case is still open to extension. But the extensibility happens in a different dimension, as described in the article - the analogue of adding more implementations of an interface is adding more switches, not adding more case clauses. That's why a switch is not a replacement for interfaces.
- lmm 6y ago> And a switch with no default case would be reported as an error if some cases are not covered. In what language? It's not an error in Scala, which is what the post is talking about.
- pkolaczk 6y agoIn Scala or OCaml you get a compile-time warning + runtime error. In Java 14, Kotlin and Rust there is a compile-time error if switch/match expression is non-exhaustive.
- lmm 6y agoYou don't get a compile-time warning in Scala, only a runtime error. https://scastie.scala-lang.org/6fGnDSzDSdmI4USN5k9OTA https://scastie.scala-lang.org/6fGnDSzDSdmI4USN5k9OTA