SKB – Scala case class unapply

Introduction

Periodically, I will publish new exercises so you can slowly build up knowledge about Scala.

It is designed to be done in a very short amount of time and learn a little bit each day, just to create a routine.

This episode will teach you about Scala case class `unapply`.

Hope you are going to enjoy it! It is designed for anyone to learn Scala from scratch and slowly learn, one Bit at a time.

After this Bit, I would love to hear your feedback in the comments down below.

Feel free to join the Discord server as well if you would like some help and support from the rest of our community.

What are we learning today?

Today we are going to learn about Scala case class `unapply` !

After so many SKBs, you pretty much are an expert !

So, let’s dive into a more advanced concept.

And particularly, about one more features of the `case class` related on how pattern matching works.

Time to try on the exercise on your own and scroll down for more information when you are done or if you are stuck.

Exercise

Here is an exercise to complete today.

If I did my job well, you should be able to guess by yourself the solution based on what you previously learned and based on the clues.

The goal of the exercise is to replace the `???` by a piece of code so that the exercise compiles and that’s how you win! Good luck!

You can fill the exercise right in here:

Or, if it does not load, go on to Scastie (pDSJjw7QQpSvUqFxWxQDlQ).

More information about Scala case class `unapply`

In this exercise you will learn (or have learned, if you have already solved the puzzle) about Scala case class `unapply`.

We have seen previously how, with `case class`, you can extract the component of it and match specific values. Have you ever wondered how it was built ? If yes, today is your lucky day ! If not, then you are going to learn it anyway.

The answer is the special method `unapply`. Similarly to `apply`, `unapply` has specific use case and behavior.

`unapply` allows you to extract parts of “something” in the context of a pattern matching.

The syntax can be very confusing. Let’s look at it in details. Everything that needs to be replace for your specific use case is in between `[ ]`.

Declaration:

```object [PATTERN_MATCHING_NAME] {
def unapply(input: [INPUT_TYPE]): Option[[OUTPUT_TYPE]] = {
// to match, must return:
Some([output_value])
// to 'pass'
None
}
}
```

We are going to see the notion of ‘pass’ in a later SKB

How it is used:

```val input: [INPUT_TYPE] = ???

input match {
case [PATTERN_MATCHING_NAME]([OUTPUT_TYPE]) => ???
}
```

What is confusing is when `[OUTPUT_TYPE]` is a tuple. Because in the `unapply` definition, it will be written something like `(Int, String)` but in the usage, in the pattern matching, it will be written `PATTERN_MATCHING_TYPE(a: Int, b: String)` and NOT `PATTERN_MATCHING_TYPE(a : (Int, String))`.

This syntax works for every pattern matching, which means it also works in `map`, `flatMap` and all.

You might imagine other possible use case for `unapply` and you are right. In our example, we only reproduced what the `case class` was doing but we can do much more. We are going to see more advanced usage of `unapply` in upcoming SKBs.

Feel free to go back to the exercise, modify the code to try out new things and get a better intuition for Scala case class `unapply`.

Conclusion

I hope you have learned something new or had fun during this Scala Knowledge Bit.