Tutorial by Examples

This example shows how to match an input against several values: def f(x: Int): String = x match { case 1 => "One" case 2 => "Two" case _ => "Unknown!" } f(2) // "Two" f(3) // "Unknown!" Live demo Note: _ is the fall th...
In standard pattern matching, the identifier used will shadow any identifier in the enclosing scope. Sometimes it is necessary to match on the enclosing scope's variable. The following example function takes a character and a list of tuples and returns a new list of tuples. If the character existed...
To check for a precise number of elements in the collection def f(ints: Seq[Int]): String = ints match { case Seq() => "The Seq is empty !" case Seq(first) => s"The seq has exactly one element : $first" case Seq(first, second) => s"The...
Case statements can be combined with if expressions to provide extra logic when pattern matching. def checkSign(x: Int): String = { x match { case a if a < 0 => s"$a is a negative number" case b if b > 0 => s"$b is a positive number" case c ...
Every case class defines an extractor that can be used to capture the members of the case class when pattern matching: case class Student(name: String, email: String) def matchStudent1(student: Student): String = student match { case Student(name, email) => s"$name has the following...
If you are matching on an Option type: def f(x: Option[Int]) = x match { case Some(i) => doSomething(i) case None => doSomethingIfNone } This is functionally equivalent to using fold, or map/getOrElse: def g(x: Option[Int]) = x.fold(doSomethingIfNone)(doSomething) def h(x: ...
When pattern matching an object whose type is a sealed trait, Scala will check at compile-time that all cases are 'exhaustively matched': sealed trait Shape case class Square(height: Int, width: Int) extends Shape case class Circle(radius: Int) extends Shape case object Point extends Shape ...
val emailRegex: Regex = "(.+)@(.+)\\.(.+)".r "[email protected]" match { case emailRegex(userName, domain, topDomain) => println(s"Hi $userName from $domain") case _ => println(s"This is not a valid email.") } In this example, the regex attem...
The @ sign binds a variable to a name during a pattern match. The bound variable can either be the entire matched object or part of the matched object: sealed trait Shape case class Rectangle(height: Int, width: Int) extends Shape case class Circle(radius: Int) extends Shape case object Point ex...
Pattern matching can also be used to check the type of an instance, rather than using isInstanceOf[B]: val anyRef: AnyRef = "" anyRef match { case _: Number => "It is a number" case _: String => "I...
The @switch annotation tells the compiler that the match statement can be replaced with a single tableswitch instruction at the bytecode level. This is a minor optimization that can remove unnecessary comparisons and variable loads during runtime. The @switch annotation works only for matches again...
The | can be used to have a single case statement match against multiple inputs to yield the same result: def f(str: String): String = str match { case "foo" | "bar" => "Matched!" case _ => "No match." } f("foo") // res0: String = M...
Given the following List of tuples: val pastries = List(("Chocolate Cupcake", 2.50), ("Vanilla Cupcake", 2.25), ("Plain Muffin", 3.25)) Pattern matching can be used to handle each element differently: pastries foreach { pa...

Page 1 of 1