SKB – Scala Generic Trait

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 Generic Trait.

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 Generic Trait !

Today is a milestone in the Object Oriented Programming journey. In combination with inheritance, that we saw in the past, generics are a key feature of Object Oriented Programming.

Once you start using it, you will never be able to live without it !

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 (1KailbBGTNCH2CDwVFK4fA).

More information about Scala Generic Trait

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

I heard the Internet likes potatoes !

In this example, we want to sum our PotatoBag. But, for some reason, we want to describe the action to Combine anything. We can describe combine easily, it is the action of taking two elements of the same type and producing a new element of the same type. You can see there that we declared a function with the typed signature: A.combineWith(A) => A.

With this in place, we can now combine anything. Try to go back to the exercise and create a different case class that inherit from the Combine[A] trait.

What is called generic in this context is the [A]. It means that this will have some kind of type parameter that will be needed at compiled time to create the class. For instance, if you were to create a Combine[Int], you can imagine the compiler writing code for you and creating a whole new class :

trait CombineInt {
  def combineWith(a: Int): Int
}
It is an other way to not have to write almost similar code several times.

Keep in mind that this example has been trim down and simplified for this exercise, in production code we would do things slightly differently but I don’t want to bring too much at once and confuse you. I am trying to make bite size knowledge pieces.

The combine pattern is use often and not far away from it you will almost always find the reduce function. In this case we use reduceOption for safety. reduceOption will return None when the list is empty, reduce will throw an exception.

You can go back to the code and replace reduceOption by reduce and remove all items from the TruckOfPotatoes to see it for yourself.

Other thing to try, .reduceOption((a, b) => a.combineWith(b)) can be replaced by .reduceOption(_ combineWith _). But, this involve infix notation as well as the placeholder _, so we are going to learn more about it later. But at least, you have been exposed to it.

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

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