Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

ScalaFP: The Mystery of Scala's Higher-Kinded Types

DZone's Guide to

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.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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.

Higher-Order Functions

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) = ???


Type System

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

Higher-Kinded Types

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.

Requirements

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 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:

Screenshot from 2018-05-10 21-46-35

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.

Screenshot from 2018-05-10 21-52-46

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.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. 

Topics:
java ,scala ,functional programming ,higher-kinded types ,type systems ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}