Scala Tutorials Part #26 - Type parameterization
Originally Posted On : 05 Nov 2017
Type parameterization
This is part 26 of the Scala tutorial series. Check here for the full series.
Index
- Introduction
- Getting started with an example
- Using custom class types
- Multiple type parameters
- Decomposing types with Generic methods
- Conclusion
Introduction
Type parameterization or otherwise called as generics combined with Scala’s type system add stability to the code. This concept is not new to Scala. Java added support for generics in J2SE 5.0 which was released in 2004. It equips the programmer with the ability to write code which is more predictable during compile time rather than time. It is heavily used in the collections API of Java and Scala.
Any type within the Java language type system can be encoded within the <>
. These are called generics and the class is just defined once using
the generics container and can be re-used.
Generics originally came from the world of Functional Programming.
Getting started with an example
We are going to design a Stack data structure from scratch. This is just an example and you would typically use something within collections when it come to actual production code. But for explanation purposes, they do just fine.
Here A
is a type parameter similar to E
used in Java collections API.
Next we will declare two variables which are private to the class.
We will implement three basic methods push
, pop
and peek
.
Our final implementation now looks like this.
Using this data structure is pretty simple.
Using custom class types
Since the type A
can be used to encode almost any type, lets try some of our custom classes.
We can then use these classes in our stack data structure.
Multiple type parameters
The Java language has an interface called Pair
which goes as
follows,
It has two generic parameters. This is used in collections such as HashMap
. Scala’s counterpart is not that different.
The []
is equivalent to <>
in terms of syntactic sugar as we saw in examples above.
Decomposing types with Generic methods
In part 23 we saw an example in which we decomposed a higher type Any
into its sub types.
We explicitly gave the type ascription to the variable typeTest
. This gets translated to type casting and it is not necessarily a good way to do this.
A better and a type safe way to accomplish this is by using a generic type signature. Let’s create an empty method which operates on generic types.
The [A]
after the method name is necessary to indicate that the method operates on generic types. Next we will do the same pattern matching inside
the method.
Now we can call this without explicit type annotation, since it is taken care by the compiler.
Prints out the following.
Conclusion
Generics are a powerful way of abstraction and writing type safe code which is more compile time reliant rather than run time. We have merely scratched the surface of what is possible with them in Scala and this article has been more like an introduction to its syntax. I have deliberately left out advanced topics such as type variance, structural typing(duck typing) which will be explained in later tutorials.
The Stack
data structure above is partially derived from the Scala lang’s official documentation guide on
generics.