July 2017
Intermediate to advanced
796 pages
18h 55m
English
One of the widely used features of Scala is pattern matching. Each pattern match has a set of alternatives, each of them starting with the case keyword. Each alternative has a pattern and expression(s), which will be evaluated if the pattern matches and the arrow symbol => separates pattern(s) from expression(s). The following is an example which demonstrates how to match against an integer:
object PatternMatchingDemo1 { def main(args: Array[String]) { println(matchInteger(3)) } def matchInteger(x: Int): String = x match { case 1 => "one" case 2 => "two" case _ => "greater than two" }}
You can run the preceding program by saving this file in PatternMatchingDemo1.scala and then using the following commands to run it. Just ...
Read now
Unlock full access