SKB – Scala _ wildcard

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 _ wildcard.

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 _ wildcard !

Few episodes ago we talked about _ in a context of use cases that I described as placeholder.

Now, let’s see the other big group of use cases that is the wildcard or the I don’t care category.

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

More information about Scala _ wildcard

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

Let’s review each of the use cases one by one

The first one is one that we have seen before in pattern matching, it is the default or wildcard match. Which is the match that match everything. Most of the time, it is used to catch errors or implement a default behavior when all the other matches have failed. Usually, you might want to put a value name instead of _ so that you can include what has failed in your error message.

The second one is to match some kind of structure, in this case, match on any Some and we don’t care what is inside. As always, you could replace the _ by some value name to be able to do something with what is inside, but if you don’t care, you can use the _ syntax.

Next one is related to the previous one, but with case class this time. We talked about unapply before. And so you might understand that the example with Some(_) is actually the same as the CaseClass(_, _, ...) . It basically, ignore some of the return elements of the unapply method.

Number 4 is related to imports. It allows you to import everything under a specific package path. Same idea, if you do not care to include specific things, you include it all.

Number 5 , even tho it doesn’t look like it, is also related. In fact, val behave like a pattern matching structure. We are going to see this more into details in a following episode.

Number 6 is similar as well, but the val is hidden inside the for-comprehension. It allows you to handle method that return Unit and still be able to include it inside the structure.

Number 7 is similar, it allows you to create behavior that ignores the input.

As you can see, _ can be used when you want to not care about a particular output.

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

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: