ScalaFP: Mystery Of Scala Higher Kinded Type.

Reading Time: 3 minutes

Scala 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 called Higher Kinded Type Or Second Order Type. Before moving to higher kinded type lets brush up our basics first.

Higher Order Function:


So, functional programming follows an artistic rule called “functions are the first class citizen“. We can pass a function as an argument to the function and those functions are called Higher Order Function like:

def function(f: Int => Boolean) = ???

Type System:

Scala type systems contain variances like invariant, 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 List[T], 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 of the 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 type parameter.

Higher Kinded Type:

Now let’s come to the simple rule,
if a function contains function as an argument, then they are called Higher Order Function. We have a similar scenario with types as well. If a type contains type constructor and that type further contains 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 our custom generic method, where we can use Higher Kinded Types.

Requirements:

I want to create a generic method, where I can pass any type which contains 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 map of Option, whereas if we pass a List, it executes the map of list and more.

Solution: 

First, let’s try to create the custom map function without higher kinded types as below:

Higher Kinded Type

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 Type.

Let’s create a utility class with Higher Kind Types.

Higher Kinded Type

In above example, we are creating a Utility class with higher kinded types and getting warning within REPL. Currently, just ignore that. But in the example, we can easily return F[B] or declare abstract of an abstract from the map method. While we are trying to create the Utility object with just passing the Option without type parameter, we are successful.

So, these are the important advantages of higher kinded type. Scala, Haskell, Purescript and OCaml language have this feature but not in Java. Using these types, shapeless, scala-cats, scalaz and more libraries are build easily. We will explore more examples of higher kinded types in our further ScalaFP or Advance Scala blogs.

References:


knoldus-advt-sticker


 

Written by 

Harmeet Singh is a lead consultant, with experience of more than 5 years. He has expertise in Scala, Java, JVM, and functional programming. On a personal front; he is a food lover.

3 thoughts on “ScalaFP: Mystery Of Scala Higher Kinded Type.3 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading