top of page

Pattern Matching in Scala: Let's Get Functional!

Written by Davide Falcone

As a former Java developer, Pattern Matching is probably the most impressive tool that Scala offered me at the beginning of my journey. It's like the friendly neighborhood superhero, always there to simplify and make your code more readable.

The Friendly Basics of Pattern Matching

Think of pattern matching in Scala as the superhero equivalent of our human "switch" statements. You've got a value and you 'match' it. Depending on the match, you may have different outcomes!

val feeling = "happy"

feeling match {
  case "happy" => println("Smiles all around!")
  case "sad"   => println("A little bit blue today.")
  case _       => println("Well, that's an enigma!")

A happy feeling prompts smiles; a sad feeling, a shade of blue. And the _ case is our wildcard, that “catches” everything that we didn’t match!

Pattern Matching with Lists

Now, let's call in the sidekick: Lists. Pattern matching gets even more captivating with lists. Fasten your seatbelts, it's time for a fun ride:

val numbers = List(1, 2, 3)

numbers match {
case List(_, _, 3) => println("Found a 3 at the tail!")
case List(_, 2, _) => println("A 2 in the middle!")
case _             => println("That’s interesting.")

Here, the _ has morphed: it matches any value, but we're not concerned about specifics.

If there’s a 3 at the end, we’ll find him;

If there’s a 2 in the middle, we cheer.

Anything else? That’s interesting, indeed!

Case Classes: Pattern Matching Plus Plus

Case classes are Scala's data containers, perfect for pattern matching due to their structural nature.

case class Coffee(name: String, strength: String)

val myCoffee = Coffee("Espresso", "Strong")

myCoffee match {
  case Coffee("Espresso", "Strong") => println("Fully charged!")
  case Coffee("Latte", _)           => println("Silk experience.")
  case _                            => println("A mysterious brew.")

Our myCoffee is a robust cup of espresso. An Espresso with Strong strength makes you fully charged. If it's a Latte, it's a silk experience. Anything else? A mysterious brew, certainly! The magic behind Pattern Matching? The unapply method. In Scala, when you declare a case class, two methods get generated : apply and unapply. - The apply method is used to create new instances of the class. - The unapply method takes an object of the case class and extracts its parameters, returning them in a tuple. This is what makes the syntactic sugar of pattern matching in case classes possible.

Wrapping Up

Think of Pattern matching in Scala as a trusty ally. It allows you to identify patterns, cleaning up your code, making it easier to read and understand. It's not only about matching values, but discovering patterns and handling them.

As we wrap up this exploration at Scala Matters, remember that pattern matching, like many aspects of Scala, isn't just about writing code - it's about writing clean, functional, and efficient code. And that is why Scala truly matters (Ba Dum Tss!).

Stay Functional!

Thank you Davide for your contribution to Scala Matters! We really appreciate it :)

If you would like to submit a blog, email us at


bottom of page