SKB – Scala higher kind

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 higher kind.

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 higher kind !

Let’s dive more into object oriented programming ideas but with a dash of functional programming.

The idea today is to go a bit further with trait and be able to describe behaviors of higher kinds. It is just a fancy terms to describe type that accept type as parameter, such as List[A], the A being the type accepted as parameter.

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

More information about Scala higher kind

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

To create a generic trait that “target” type that take type(s) as parameter, the syntax is:

trait MyTrait[F[_]] {}

The F[_] means that we are expecting a type F that take one type parameter as argument, the _.

Note that it is possible to target a type that takes two types as input with the following syntax: F[_, _]. You can imagine that it is possible to target type with more than two following the same logic.

When you use a parameterized type you do something like List[Int] so if you want to use the type itself you simply has to do Trait[List], not need to add anything after the List.

You also noticed that you can create your own type and it will behave the same way. Look at CounterT in our example. It takes one type as parameter so it can be used with our trait.

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

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: