Scala for Java Refugees Part 4: Pattern Matching and Exceptions - Code Commit
One of the major limitations of switch/case in Java (and really any C derivative language) is that it can only be used on primitives. You can't use switch/case to test a String, for example (a need which arises more often than one would think). In fact, the most complex type testable within switch/case is the Enum, and even this is just being reduced to its ordinal values under the surface. In short, switch/case is a very crippled hold-over from the dark ages of #include and extern.
Fortunately, the designers of Scala chose not to include this "feature" in their new language. Instead, they implemented a "new" (it's actually been around in other languages for a long time) concept called pattern matching. At a basic level, it allows algorithms which are very similar to the checkPrime(int) example:
Read full article from Scala for Java Refugees Part 4: Pattern Matching and Exceptions - Code Commit
No comments:
Post a Comment