Introduction

Type Systems perform an important role for removing code duplication, run-time safety for reducing program errors and more. Initially, when I was learning Java, I saw generics which help us to use the collections in a type-safe manner for avoiding runtime problems like:

// create a list with the integer value
ArrayList list = new ArrayList();
list.add(1);

// read the list and cast the value into the string
(String) list.get(0); // Runtime Exception ClassCastException

These were the general problems which most developers faced in their day to day coding. Before generics, the List add method accepted the Object type and the get method returns the Object type, so if we really want to use some specific behavior of the added element's in the list, we'd have to cast that object manually. For resolving these problems generics comes into the picture and save the developer's life. I was really happy to use collections with generics like:

// add the integer to the list
ArrayList<Integer> list = new ArrayList<>();
list.add(2);

// read the values from the list
Integer value = list.get(0);

Now things are more safe and clean and without worry of runtime exceptions. After that, I started to learn about generics and how we can create our custom classes via generics with their use-cases like the DAO layer. But when I moved to Scala, I saw a huge feature list of the Scala type system. These features are split into a chapter with small and crisp examples. We are trying to cover all of those features which are mostly used for developing Scala libraries and help us to contribute to Scala open source projects.

Last updated