Scala Pattern Matching

Learn via video courses
Topics Covered

Overview

Pattern matching in Scala is a powerful feature that allows you to match values against specific patterns and execute code based on the matched pattern. It is similar to the switch or case statements found in other programming languages, but it offers more flexibility and expressiveness.

Introduction

Pattern matching is one of the most powerful and distinguishing features of the Scala programming language. It provides a concise and expressive way to handle complex data structures like literals, case classes, tuples, and collections. Using the match keyword, we can execute code based on matched patterns, making code more readable and maintainable. Pattern matching plays a fundamental role in functional programming, enabling elegant solutions to complex problems and making Scala a versatile language for expressing data transformations and algorithms.

Syntax

Pattern matching in Scala is a technique` used to identify and check a value against a set of patterns and then execute code based on the matched pattern.

The syntax for pattern matching in Scala is:

Here's a breakdown of the elements in the pattern-matching syntax:

  • valueToMatch: The value that you want to match against the specified patterns.
  • case pattern =>: The individual case statements, where pattern is the pattern that you want to match against valueToMatch. If pattern matches valueToMatch, the corresponding code block will be executed.
  • =>: The arrow symbol separates the pattern from the code to execute.
  • case _ =>: This is the "catch-all" or default case, where the underscore is a wildcard that matches any value. If none of the preceding patterns match, this code block will be executed.

Matching on Case Classes

Case classes are a natural fit for pattern matching in Scala because they automatically generate extractors for their fields. This allows us to match the structure of the case class easily.

Here's how we can match on case classes using pattern matching in Scala:

  • Define a case class:
  • Create an instance of the case class:
  • Use pattern matching to extract fields from the case class:

In the example above, the first pattern Person("Alice", age) matches if the name field is "Alice", and it extracts the age field. The second pattern Person(name, _) is a catch-all and matches any Person instance, binding the name field to the variable name. If none of the patterns match, the code won't execute.

Pattern guards

Pattern guards provide a way to add additional conditions to the patterns, allowing for more fine-grained matching. It lets us define boolean expressions that must be true for a pattern to be considered a match. Pattern guards are denoted by adding an if clause after the pattern.

Syntax:

Let's see an example of using pattern guards:

In the example above, we use pattern guards to check if the matched number x is even or odd. The if clause after each pattern specifies the corresponding guard expression. Here since the value of x is an even number, it will match with the even case. However, before the pattern is considered a match, the pattern guard expression if even % 2 == 0 is evaluated. If the guard expression is true, the code inside the block println(s"$even is an even number") will be executed. In this example, x is 10, which is even, so this case will match, and the output will be "10 is an even number".

Matching on Type Only

In Scala, we can use the match statement to match on types without necessarily extracting any values from the matched object. This type-only matching is useful when we want to perform different actions based on the type of an object, without caring about its specific fields or values.

We can use the wildcard _ as the pattern and specify the type after it. This allows us to execute different code blocks based on the type of the matched object.

Here's an example of matching on type only:

In the example above, the process function matches the type of the obj parameter using the _ wildcard. It identifies the type of obj and prints an appropriate message based on the type.

Sealed types

In Scala, the sealed keyword is used in combination with match statements to provide better support for exhaustive pattern matching. When a class or trait is marked as sealed, it means that all its subclasses are known, and the compiler can perform exhaustive checks to ensure all possible cases are handled in pattern matching.

Examples:

  • Sealed Classes:

In the example above, Animal is a sealed abstract class, and Dog and Cat are its subclasses. Since Animal is marked as sealed, the compiler will warn you if you forget to handle any possible subclass of Animal in the animalSound function.

  • Sealed Traits:

In this example, Result is a sealed trait, and Success and Failure are case classes that extend the trait. When pattern matching on Result, using sealed ensures that all possible implementations of Result (i.e., Success and Failure) are covered in the processResult function.

Conclusion

  • Pattern matching in Scala allows us to match values against specific patterns and execute code based on the matched pattern, providing a concise and expressive way to handle data structures like literals, case classes, and collections.
  • Matching on class in Scala involves pattern matching against an object's type, allowing different actions based on the class of the object, often used for handling polymorphic behavior and data structures.
  • Pattern guard in pattern matching in Scala lets us apply additional conditions to the matched patterns, allowing more specific and fine-grained matching based on boolean expressions.
  • Matching on type only in pattern matching in Scala involves using a wildcard pattern _ with the type to execute different code blocks based solely on the object's data type, without extracting any values.
  • In Scala, sealed is used to mark classes or traits to restrict their inheritance hierarchy to a specific scope.