SKB – Scala unapply magic


This article is part of the Scala knowledge bits Series.

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 unapply magic.

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 unapply magic !

I personally love the unapply method. Not many people talk about it but I think it has really cool features.

A somewhat long exercise today but it will give you a great tool to use in the future !

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


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.

But if you get stuck, scroll down to get more information.

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 (3dUlzSLfRMqUFGOQ1Ms8vA).

More information about Scala unapply magic

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

Let’s recap quickly what unapply was used for as far as we know for the moment.

In the previous episodes, we saw unapply in the context of case class to be able to extract the fields.

The syntax is:

object HANDLER {
    def unapply(input: INPUT_TYPE): Option[OUTPUT_TYPE] = {
         // when return value is "some", it is outputed
         // when return value is "none", this match is pass

And how it is used:

input match {
     case HANDLER(OUTPUT_TYPE) => ....

As you can see, the input of unapply is what enter the pattern matching structure and the output is an Option of what is being returned from the match.

Several interesting things that you can do with unapply:

  • Customize the input
  • Customize the output
  • Customize the name
  • And the most interesting part to me, you can yield the match to the next matching condition.

The last bullet to me is the most important one. It is a great tool to contain responsibilities. The Option output allows you to return something when you know what to do with it or return None when you don’t know and the higher abstraction level will make decisions on how to handle the situation. It is really powerful to me because that allows you to “fail” without failing. And try several things before giving up. Let me go into details.

    val output: OUTPUT_TYPE = input match {
        case Attempt1(result) => result
        case Attempt2(result) => result
        case Attempt3(result) => result
        case n => // when all else has failed

This pattern that I use often in parser for examples, allows you to add new handler very easily. Also, each parser should be very simple, returning None when nothing can be done without having to care for the rest.

One thing to be very aware/careful about is the order, because in this structure, the first match that works will be the results. If one of your matcher is more specific than an other one, lift it up first in the order of test or a larger condition might yield true before it.

If you want to talk more about one of my favorite Scala method: unapply, jump on the Discord channel!

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


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

Please ask questions or post feedback in the comments below.

Feel free to try on the next Scala Knowledege Bit.

If you are curious about the previous Scala knowledge Bits, go check it out! 🙂

Leave a Reply

%d bloggers like this: