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 the difference between val
, lazy val
and def
in Scala.
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 the difference between val
, lazy val
and def
in Scala !
We have seen val
and def
in previous SKBs but we haven’t encountered the keyword lazy
before.
Try to figure out by yourself what lazy
does. Make sure to read the print statements in the console as you run the code.
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 (fRqDtrL9Q22S2aB2Pue9mg).
More information about the difference between val
, lazy val
and def
in Scala
In this exercise you will learn (or have learned, if you have already solved the puzzle) about the difference between val
, lazy val
and def
in Scala.
Did you figure it out ?
The keyword lazy
allows a value to not be evaluated until the very first time it is called. If you call the value again, the previously computed value will be used.
With val
the value is computed instantly as the value is declared. You were able to see the print statement early in the log, before all the others.
And for def
, this is a method, so each time you call the method, you are recomputing the result.
Summary:
val
: Computed once when declaredlazy val
: Computed once when calleddef
: Computed each time it is called
Feel free to go back to the exercise, modify the code to try out new things
and get a better intuition for the difference between val
, lazy val
and def
in Scala.
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! 🙂
I’m not familiar with next SKB’s so the answer to my question might be out there but as for now:
I figured out what’s the difference between lazy and regular val but it would be nice to provide some examples/scenarios when it’s better to use lazy val.
Personally, I use
lazy val
almost everywhere. I would only useval
when I want something to fail fast.lazy val
is so powerful, it guarantee that things won’t be instantiated until they are needed. It is useful in test and in optimization.