SKB – Scala implicit val

SKB – Scala implicit val

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 implicit val.

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 implicit val !

implicit val can do a lot but, for now, we are just going to learn about the basic use case.

This exercise has a lot of code not related to what we are learning, but I am trying to illustrate some kind of real use case rather than a one line exercise. I think it makes it more exciting. If you disagree, please come let me know on our Discord community !

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 (2u0HgFdmQhmH0WP6aM1dNA).

More information about Scala implicit val

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

I made sure to reuse only parts we have seen before, and we have seen a lot ! Congratulations for going this far on this series, I hope that it is beneficial to you ! If there are specific things you would like to learn, just let me know and I’ll add to the TODOs of episodes to write.

You can see the use of case class, methods with def, private, object but the new thing here is implicit val.

In a method, you would declare it like any other argument. Except, all implicit arguments must be in their own “bucket” of arguments and this “bucket” must be the last one of them all for this method. And you have to precede the list of argument by the keyword implicit. Like this:

def methodName(arg1, arg2, arg3)(arg4, arg5)(implicit arg6, arg7, arg8): OutputType = ???
Each argN would be the name: Type combo.

Now, when you actually call the method containing the implicit argument, you do not have to give a specific argument if one is present in the context. You can see it declare like this:

implicit val name: Type = ???
Note that in this example it was set to private.

You can also overwrite or just pass the argument like any other normal arguments.

Note that you shouldn’t abuse this feature. In fact, it can become dangerous as well as make the code extremely hard to read. Imagine the extreme case where every arguments are implicit, it would be extremely hard to know what is happening. It makes the code hard to read in static environments like GitHub. And, in case of ambiguous possibilities, it can be tricky to understand what is going on.

I would recommend to only use this feature for elements that would be part of a configuration or such. Elements that would have to be copy paste and pass to each and every functions down the line. Only use it if it makes the code more readable.

With great power comes great responsibility.

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

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: