ScalaFP: The Mystery of Scala's Higher-Kinded Types
Here's a good overview of higher-kinded types in Scala, their uses, and a few code examples of putting them to use.
Join the DZone community and get the full member experience.Join For Free
Scala's type system holds a lot of mysteries. Sometimes, these mysteries confuse us, but mostly they provide us with a powerful feature. Today, we will be discussing one of them — higher-kinded types or second order types. Before moving onto higher-kinded types let's brush up on our basics first.
So, functional programming follows an artistic rule that states, "Functions are first-class citizens." We can pass a function as an argument to a function, and those functions are called higher-order function like:
def function(f: Int => Boolean) = ???
Scala's type system contains variances like covariance and contravariance, but the higher-kinded type is different from them. We can either create custom type classes in Scala or we can use some of the existing Scala type classes like
Option[T], and more. We are pretty familiar with that syntax and here T means abstract so, as per our requirements, we will decide what T is. In some cases, there might be a requirement for nested hierarchies like
List[List[T]], where T can be easily replaced with
List[T], which further contains a type parameter.
Now a simple rule: If a function contains a function as an argument, then it is called a higher-order function. We have a similar scenario with types as well. If a type contains a type constructor, and that type further contains a type constructor like...
class Abstract[U[T]] or class Abstract[U[_]]
...they are called higher-kinded types. Scala provides this beautiful feature for building generic libraries.
Let’s create a custom generic method where we can use higher-kinded types.
I want to create a generic method where I can pass any type that contains a
map method. According to the passed type, like an
Option or a
List , it performs the
map operation on the passed expression and returns the result. For example, if I am passing an
Option, it executes a
Option, whereas if we pass a
List, it executes the
map of List and more.
First, let’s try to create the custom map function without higher-kinded types as below:
If you look at the above example, we are creating the class with two type constructor F and A and declare a map method which accepts a higher order function. The main problem with this code is, map method returns F only. Our F can be anything, which contains map method. In that case, if we pass
Option as an F we are getting an error because
Option itself contains type parameter like we saw in the example. We are trying to define the Utility for
Option of type
Int so we get an error.
In that type of cases, we usually get stuck. For solving these types of issues, we have a feature called higher-kinded types.
Let’s create a utility class with higher-kinded types.
In the above example, we are creating a
Utility class with higher-kinded types and getting warning within the REPL. Currently, just ignore that. But in the example, we can easily return F[B] or declare an abstract of an abstract from the map method. We are trying to create the
Utility object by just passing the
Option without a type parameter, and we are successful.
So, these are the important advantages of higher-kinded types. Scala, Haskell, PureScript, and OCaml have this feature, but not in Java. Using these types, shapeless, scala-cats, scalaz, and other libraries are easily built. We will explore more examples of higher-kinded types in further ScalaFP or Advanced Scala blogs.
Published at DZone with permission of Harmeet Singh(Taara). See the original article here.
Opinions expressed by DZone contributors are their own.