Intive-FDV Blog

Logbook of Java Coders Writing in Scala

What’s a Match Case?

Professionals who come from the world of Java and other languages are quite familiar with the switch statement, which allows to nest multiple if statements but can only be used with ordinal numbers. In Scala there is the match case statement, more complex than the switch statement and with other possibilities.

How Does it Work?

Let’s look at a few examples.

This is an example of a simple switch statement. There is a case for each possible option, with its corresponding path. Based on the number obtained for x, we get a string. If the x number isn’t between 0 and 2, it falls within the case _.  For the match case, _ is the default value.

In addition, the match case can also work with classes. Let’s see how.

Here, we can see that “Notification” is an abstract class from which the “Email”, “SMS” and “VoiceRecording” classes are derived. If we needed to use a match case, we could write something as follows:

As we can see, the function gets a “Notification” object and the subclasses, where its constructor is used, are evaluated.

But it doesn’t end there. Match cases can be combined with other types of statements to make them more specific. For example:

Differences with the Switch Statement in Java

At this point, we may think that a match case is similar to a switch statement in Java, but there are subtle differences between them:

a. Unlike the switch statement, the match statement is an expression. That’s why it always produces a value, in a true functional way.

b. There is no need to use a break statement after each case, so we don’t have to remember to include it.

c. If none of the cases match, the match case triggers a MatchErrorException, and we don’t have to forget the default case _.

Match Case Patterns

In Scala, the match case is known as the match pattern. We can use different patterns for cases:

1. Constant Patterns

This would be the switch statement in Java:

2. Variable Patterns

They allow us to include case variables:

3. Sequence Patterns

They allow us to include List or Array. For example, we can write a match statement for a list starting at zero with a variable length:

4. Tuple Patterns

We can also write a match statement by using tuples:

5. Typed Patterns

Lastly, but no less useful, we can also write a match statement based on the data type:

Conclusions

Why is this Scala structure so useful, compared to the classic switch statement?

  • It’s not limited to native data types, allowing for more complex and specific combinations in our code.
  • Given that we can use patterns, data can be extracted and re-used in other statements.
  • Other comparison and repetition statements can be used in the same pattern matching, like for and if statements, etc.
  • Match cases are expressions, not statements, and they allow to evaluate and assign results. This is essential for functional programming.

 

References:

https://docs.scala-lang.org/tour/pattern-matching.html

https://alvinalexander.com/scala/how-to-use-pattern-matching-scala-match-case-expressions

Alejandro Sandler

Alejandro Sandler is a developer at intive-FDV since October 2018. Computer engineer graduated from the Universidad de La Matanza (UNLaM), Alejandro has a deep interest in the learning process, as a student as well as on the teaching side, an area in which he plans to venture into the near future. A music lover, he attends recitals frequently. In addition, among his hobbies are watching series, traveling and reading about software.

Mariana Silvestro

Mariana Silvestro is a full-stack developer in intive-FDV since December 2017 and leader of the Backend team since October 2018. With a degree in Computer Science, graduated from the Universidad Atlántida Argentina, Mariana is also a Senior Technician in Information Systems from the Universidad Tecnológica Nacional (UTN). Member of the LasDeSistemas community, she is a great militant of feminism and gender equality inside and outside the IT industry. Intensely passionate about reading and writing, she has 4 poems and 3 micro-stories published.

Add comment