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 Enumeration for 2.x.
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 Enumeration for 2.x !
Time to assemble everything we learned in the recent episodes !
We are going to use
, pattern matching,
What you are about to see here is good knowledge to keep but the exact use case and application showed here will be deprecated in Scala 3. However, the underlying features and logic we are using will still be here so it is not a loss !
Time to try on the exercise on your own and scroll down for more information when you are done or if you are stuck.
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 (RpzCdEpvSnKsBpI1TsckHw).
More information about Scala Enumeration for 2.x
In this exercise you will learn (or have learned, if you have already solved the puzzle) about Scala Enumeration for 2.x.
We are writing an Ore management software today.
Notice how we are able to describe the Ores with a finite number of possibilities. If we want a new one, we need to add it ourselves. Did you notice the warning about pattern matching :
warning: match may not be exhaustive.Since we are using
sealed, the compiler can know exactly the number of options available for this type. So the compiler can help us !
We could simply have a
of course. But noticed the typo in
? And the compiler caught it for us !
The rest of the exercise is just to go a bit further and play with what we built.
You can set up a system for reporting, and add handy calculation with
You can also set up special case like for Iron in the last example.
Something that needs to be said is that in Scala 3 this pattern will be completely changed. Scala 3 will implement its shortcut version for Enumeration that will pretty much write this boiler plate code for us. But all those features to customize it yourself will still be available so it is not a loss to know about it !
I hope this little exercise with a more applicable example was fun! If you have any feedback, as always, don’t hesitate to comment below or message me on the Discord server. Looking forward to hearing from you.
Feel free to go back to the exercise, modify the code to try out new things and get a better intuition for Scala Enumeration for 2.x.
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! 🙂