SKB – Scala contexts

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

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

We are going to formalize something we saw before but without stopping and looking at it.

Contexts are defined by { ... }.

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

More information about Scala contexts

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

You realize that we saw context before. We saw the characters { and } in the previous SKBs.

We saw it before when we defined a method using def. We also saw it when we defined a class, an object, even a val.

If you use a val within a context, you are going to be using the one the closest to the context you are in. Keep in mind that it is not good practice to reuse the same name over and over as it makes the code extremely confusing and hard to debug. But this example is trying to illustrate to scope of the val based on inside which context is has been declared and is being used.

Context always return a value. You can see that in the very first context defined by

1
This context is implicit, and could be re-written to:
{ 
   1
}
This is the same concept for each val:
val a: Int = 1
can be read as
val a: Int = {
   1
}

Context always return the last line of code as its overall returned value. The other aspect to know about is related to the garbage collector inherited from Java. Yes, sometimes we have to remember that Scala is based on the JVM and inherits some of its core features, one of them being the garbage collector.

The idea behind the garbage collector is the memory management. Each time you create a val you need to allocate memories to keep it “alive”. That is all well and good but if you keep creating more and more things, your computer is going to die. How do you know you can remove a value from the memory ? Context !

When the program is leaving a context, it knows it can safely remove all values that were declared in this context and free the memory.

I hope this SKB was helpful in understanding better the structure of a Scala program. As always, don’t hesitate to message me or any member of our community if you need help !

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

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: