DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. ScalaFP: The Mystery of Scala's Higher-Kinded Types

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.

Harmeet Singh(Taara) user avatar by
Harmeet Singh(Taara)
·
May. 18, 18 · Tutorial
Like (8)
Save
Tweet
Share
18.82K Views

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.

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.

Scala (programming language)

Published at DZone with permission of Harmeet Singh(Taara). See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • ChatGPT: The Unexpected API Test Automation Help
  • Express Hibernate Queries as Type-Safe Java Streams
  • How to Secure Your CI/CD Pipeline
  • A Brief Overview of the Spring Cloud Framework

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: