When you code in Java, you can use a Switch statement. However, in Scala, we have a more powerful feature called Pattern Matcher, which is a kind of switch but on steroids.
Following is a Simple Pattern Matcher in Scala:
$ scala Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_77). Type in expressions for evaluation. Or try :help. scala> def resolve(choice:Int):String = choice match { | case 1 => "yes" | case 0 => "no" | case _ => throw new IllegalArgumentException("Valid arguments are: 0 or 1. Your arg is: " + choice) | } resolve: (choice: Int)String scala> println(resolve(0)) no scala> println(resolve(1)) yes scala> try { | println(resolve(33)) | } catch{ | case e:Exception => println("Something Went Worng. EX: " + e) | } Something Went Worng. EX: java.lang.IllegalArgumentException: Valid arguments are: 0 or 1. Your arg is: 33 scala>
Scala uses Pattern Matcher for error handling. Java does not have Pattern Matcher like Scala. It's similar to a switch statement; however, Pattern Matcher can be used in a method return statement as you can see in the preceding code. Scala developers can specify a special operator called _
(Underscore), which allows you to specify anything in the Pattern Matcher scope. This behavior is similar to else
in an if
conditional. However, in Scala, you can use _
in several places, and not only as the otherwise clause, like in Java switch.
Error handling in Scala is similar to error handling in Java. We use try...catch blocks. The main difference is that you have to use Pattern Matcher in Scala, which is great because it adds more flexibility to your code. Pattern Matcher in Scala can operate against many data structures like case classes, collections, integers, and strings.
The preceding code is pretty simple and straightforward. Next we will see a more complex and advanced code using the Scala Pattern Matcher feature.
Following is an Advanced Pattern Matcher using Scala REPL:
$ scala Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_77). Type in expressions for evaluation. Or try :help. scala> def factorial(n:Int):Int = n match { | case 0 => 1 | case n => n * factorial(n - 1) | } factorial: (n: Int)Int scala> scala> println(factorial(3)) 6 scala> println(factorial(6)) 720 scala>
Pattern Matcher can be used in a very functional way. For instance, in the preceding code, we use the Pattern Matcher for recursion. There is no need to create a variable to store the result, we can put the Pattern Matcher straight to the function return, which is very convenient and saves lots of lines of code.
Following is an Advanced complex Pattern Matcher using Scala REPL:
$ scala Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_77). Type in expressions for evaluation. Or try :help. scala> trait Color defined trait Color scala> case class Red(saturation: Int) extends Color defined class Red scala> case class Green(saturation: Int) extends Color defined class Green scala> case class Blue(saturation: Int) extends Color defined class Blue scala> def matcher(arg:Any): String = arg match { | case "Scala" => "A Awesome Language" | case x: Int => "An Int with value " + x | case Red(100) => "Red sat 100" | case Red(_) => "Any kind of RED sat" | case Green(s) if s == 233 => "Green sat 233" | case Green(s) => "Green sat " + s | case c: Color => "Some Color: " + c | case w: Any => "Whatever: " + w | } matcher: (arg: Any)String scala> println(matcher("Scala")) A Awesome Language scala> println(matcher(1)) An Int with value 1 scala> println(matcher(Red(100))) Red sat 100 scala> println(matcher(Red(160))) Any kind of RED sat scala> println(matcher(Green(160))) Green sat 160 scala> println(matcher(Green(233))) Green sat 233 scala> println(matcher(Blue(111))) Some Color: Blue(111) scala> println(matcher(false)) Whatever: false scala> println(matcher(new Object)) Whatever: java.lang.Object@b56c222 scala>
The Scala Pattern Matcher is really amazing. We just used an if
statement in the middle of the Pattern Matcher, and also _
to specify a match for any kind of red value. We also used case classes in the middle of the Pattern Matcher expressions.