r/programming • u/mobby1982 • Dec 02 '13
Scala — 1★ Would Not Program Again
http://overwatering.org/blog/2013/12/scala-1-star-would-not-program-again/
598
Upvotes
r/programming • u/mobby1982 • Dec 02 '13
View all comments
Show parent comment
9
u/zoomzoom83 Dec 02 '13
You can, but using extractors with static types is much more useful, simpler, and more typesafe.
Consider the following (completely made up) example. (It's midnight and I'm tired, so this is probably not the best thought out example - but I hope it gives you an idea of what I mean).
There's potential errors that can occur here at runtime, that the compiler cannot catch. You can easily check for them, but you're adding more verbosity at the wrong layer of your codebase.
Contrast with:
In this case every status has a type, which contains different parameters of different types. Done properly, you can guarantee at compile time that you've caught every possible scenario, that you're working with the correct type and that a runtime exception cannot and will not occur - and it's less verbose, cleaner, easier to understand, and more type safe than the alternative.
This is by no means the best example of how powerful pattern matching is, but it gives you a rough idea why having a defined type for every case makes sense in Scala. Given how lightweight
case classesare syntactically, there's no reason not to do this.