Pattern Matching In Scala | Basic & Advanced Pattern Match

Reading Time: 5 minutes

Pattern Matching is one of the most powerful features in SCALA. Unlike in C, C++, JAVA we have a switch statement, Scala has a rich set of “patterns” for matching. So, In this blog, we’ll discuss various types of pattern matching followed by some advanced concepts in Pattern Matching in Scala.

Pattern Matching In Scala –

Pattern Matching is a mechanism of checking a value against a Pattern. It gives a way of checking the given sequence of tokens for the presence of a specific pattern. Here, we match expressions against a pattern.

Compared to the ‘switch’ in C++, C, JAVA, there’s no fall through to the next alternative in Scala pattern matching. A Match error is thrown when no pattern matches.

Types of Pattern Matching in Scala –

WildCard Pattern –

This type of pattern is used to match against any case or any object. It doesn’t depend upon the types or value of the pattern rather than it allows to match all types of objects. But it is recommended to use such type of pattern as an alternative.

Variable Pattern –

This type of Pattern is quite similar to the WildCard Pattern but in this pattern matching, we are allowed to bound the value of an object to a variable so that we can use the value wherever required.

Typed Pattern –

Variable Patterns can be used to bound objects to a variable but what if, we need to bound the variable to a specific data type. Then, Typed Pattern comes into the picture. Here, we bound those object-mapped variables to specific data types.

Constant Pattern –

As the name suggests, matching against the Constant. This type of Pattern is used to match a pattern against a Constant.

Stable Pattern –

Likewise, we have Constant Pattern, where the pattern matching is done against a Constantbut what if the constant is being declared somewhere in program. Then in such case we’ll use Stable Pattern to match against a variable. In case we are gonna use ‘“’ symbol for the same.

Tuple Pattern –

Till now we have discovered how to do a pattern matching in different ways but only for a single value or object but what if we have multiple number of objects or values to match against a pattern. Such type of Pattern Matching can be done through Tuple Pattern Matching.

Constructor Pattern –

Case Class which is one of most beautiful feature of Scala to work on. For Pattern matching the same, we use Constructor Pattern Matching.

Sequence Pattern –

This type of Pattern is used when expression needs to matched against a Sequence with some particular cases.

Pattern Alternatives –

What happens is if you have a number of cases to handle in a pattern matching and somehow some cases have the same actions to perform then we can do this through Pattern Alternatives. With the help of this, we can perform the same action in different cases in a single case.

Pattern Binders –

Now, we have another scenario where we have to bind the entire case class into a single variable. In such cases, we’ll use the pattern binders to bound the case class into a single variable and thus we can use the variable wherever required.

Advance Concepts in Pattern Matching in Scala

Till now we have learned how to deal with different types of pattern matching in Scala. But what if we have to match a class or on basis of any of its parameters.

Let’s take an example of a class to demonstrate the same.

class Employees (val name: String, val age: Inr, val id: Int)

In order to do Pattern Matching on class, we’ll first declare an object with any name i.e. no constraint with the class name. After declaring an object, we are gonna use a method named unapply with instantiating the object of class as shown below –

object Employee{

  def unapply(employees: Employees) : Option[(String, Int, Int)] =

    Some((, employees.age, employees.Id))


This unapply method returns an option of tupples with some values, (later on, will also discuss the custom return type of unapply method in some another blog). Now, that’s all to do. Let’s do the pattern matching of a class using the object which we have created as shown –

val Employee1 = new Employees("Knolder", 22, 1471)

Employee1 match {
  case Employee(name, age, id) => println(s"name is ${name} age is $age id is $id")

So, what we have done is, we have created an object of class Employees as Employee1 with name as ‘Knolder’, age as ’22’ and id as ‘1471’. And after doing so, we are performing pattern matching on it and this time the case we’ll use to do so will be the object Employee. In the same way we also can do matching on some specific params of the class, rather than doing on the entire class.

Conclusion –

So, in this blog we have discussed about all the basic types of pattern matching in Scala, also we have learned a bit deeper by doing it on a class using a unapply method.

Scala Future

Written by 

Kuldeepak Gupta is a passionate software consultant at Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. His current passions include utilizing the power of Scala, Akka, and Play to make Reactive and Big Data systems. He is a self-motivated, enthusiastic person who is recognized as a good team player, dedicated, responsible professional, and a technology enthusiast. His hobbies include playing hockey, participating in Political debates, Reading Tech blogs, and listening to songs.

1 thought on “Pattern Matching In Scala | Basic & Advanced Pattern Match6 min read

Comments are closed.