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 multiple inheritance.
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 multiple inheritance !
We talked about inheritance in the past.
Let’s see how we can combine several trait
together and what are the limitations.
For this episode, finish the exercise once and then go back and follow the extra instructions in the comments if you have time to learn more.
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 (QpCO0GjsTrKBo4wGdIpEJA).
More information about Scala multiple inheritance
In this exercise you will learn (or have learned, if you have already solved the puzzle) about Scala multiple inheritance.
The keyword of the day is with
.
This article is for scala 2.x, things are changing in scala 3.x but the concepts are still the same. At the time of this article, Scastie, which I use to render the exercise do not support Scala 3 yet so we are going to revisit those concept and the new syntaxes in the future.
We saw extends
already, when we want to add one more trait
to the mix, we use with
for the one after the first one.
[case] class MyClass(args....) extends TRAIT1 with TRAIT2 with TRAIT3 .... { }
If you have followed the extra instructions in the comments after you were done with the exercise, you noticed that trait
and abstract class
behave differently.
You have to put the abstract class
in first position and you can only have one present. It can seems weird but in practice, it is rarely an issue since you can convert the abstract class
to a trait
very easily. You would just move the input arguments to the body of the class
and make them methods with def
.
You might have also noticed that you can mix two or more trait
that have the same method(s) defined as long as they are identical. But it stops working if they have methods with the same name but different types.
And the last bits to keep in mind is when using defined type, you cannot use that has a shortcut to create larger trait
. But you can use it in the position of type. Let me explain:
type FOO = A with B // this do no work: trait BAR extends FOO // this works: trait BAR extends A with B // this works: val f: FOO = ???
If you want to create bundle of trait
, you can simply make a new trait
trait FOO extends A with B trait BAR extends FOO val f: FOO = new BAR {}
And that is it for mixing several trait
and handle multiple inheritance. As always, if you have questions or feedbacks, jump on the Discord server ! See you there.
Feel free to go back to the exercise, modify the code to try out new things and get a better intuition for Scala multiple inheritance.
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! 🙂