SKB – Scala extractor pattern

Introduction

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 extractor pattern.

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 extractor pattern !

Today we are going to combine several things we know to learn about extractor pattern.

It might be a bit complicated but I am sure you are going to be fine !

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.

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 (m2wkxempSUqHNni7KEz1Zg).

More information about Scala extractor pattern

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

The goal here is to be able to only keep the element of the List that are of a given type.

We start from a list of type A : List[A]. And we want to keep the element of type B, with B being the child class of A. We saw few exercise ago the syntax : B <: A.

We also saw the implicit proof using the syntax: A : PROOF. In this case we want B to be a ClassTag as well as being a child of A. Which is why we now have this syntax: B <: A: ClassTag.

Have you tried removing the ClassTag ? You are seeing a Warning talking about abstract type B is unchecked since it is eliminated by erasure. We are going to dive deeper into this in another episode. But for now, you only need to know that to mitigate the "type erasure" problem, we need to introduce a proof to be ClassTag.

Now that we have our extractor, we can filter down the List[Animal] to only keep the Cat or the Dog !

I am sure you are going to need this pattern in one of your project one day ! Just let us know on the Discord server how that has been useful to you !

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

Conclusion

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: