SKB – Scala *-parameter

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 *-parameter.

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 *-parameter !

Let’s see how we can pass an array as argument smoothly.

We are going to see an example when this syntax shine the most: nested Node creation.

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

More information about Scala *-parameter

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

Do you experience the pain of writing Node(List(Node(List(.... Not pleasant huh ?!

But never again you will have to do this now that you know about * !

Let’s see how it works:

In a function, you can have TYPE* to take all the last parameters given to the function and turn it into an array. If you place the argument of the method with TYPE* not as last argument you will get an exception *-parameter must come last.

After doing this, instead of doing foo(List(a,b,c)) you are able to do foo(a,b,c). And inside the body of the method you can use the parameter the same as before.

But now, let’s say in some part of your code you do have a List already but you have changed the method, you want to convert the List into a *-parameter.

The weird syntax to do this is: foo(list:_*). It means you are casting (changing the type) , with : into _ which is a wildcard, so anything, in this case, the anything being the type A of the list which should be the same as the input type of the method A*, and finally *.

If we look at the type only we are doing this:

list      : _*
List[A] : A*

If I didn’t explain well, please feel free to reach out on the Discord server so we can improve the content of this article together.

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

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: