Getting Started with Scala Cats
In this article, we'll get started with Cats, which is one of the functional libraries of Scala.
Join the DZone community and get the full member experience.Join For Free
This blog was first published on Knoldus Blogs
This article is about Scala Cats. It is a library which provides abstraction for functional programming in the Scala programming language. The name is a playful shortening of the word category.
Cats is a lightweight, modular, and extensible library for functional programming.
Cats contains a wide variety of functional programming tools and allows developers to pick the required ones. The majority of these tools are delivered in the form of type classes that we can apply to existing Scala types.
What are Type Classes?
Type classes are a programming pattern originating in Haskell. They allow us to extend existing libraries with new functionality, without using traditional inheritance, and without altering the original library source code.
There are three important components to the type class pattern: the type class itself, instances for particular types, and the methods that use type classes.
What is a Type Class?
A type class is an interface or API that represents some functionality we want to implement. In Scala a type class is represented by a trait with at least one type parameter.
For example, we can represent generic “serialize to JSON” behavior as follows:
What are Type Class Instances?
The instances of a type class provide implementations of the type class for specific types we care about, which can include types from the Scala standard library and types from our domain model.
In Scala we define instances by creating concrete implementations of the type class and tagging them with the implicit keyword:
How to use a Type Class?
A type class use is any functionality that requires a type class instance to work. In Scala this means any method that accepts instances of the type class as implicit parameters.
Cats provides utilities that make type classes easier to use, and you will sometimes seem these patterns in other libraries.
There are two ways it does this: Interface Objects and Interface Syntax.
What are Interface Objects?
The simplest way of creating an interface that uses a type class is to place methods in a singleton object:
To use this object, we import any type class instances we care about and call the relevant method:
The compiler spots that we’ve called the toJson method without providing the implicit parameters. It tries to fix this by searching for type class instances of the relevant types and inserting them at the call site:
What is Interface Syntax?
We can alternatively use extension methods to extend existing types with interface methods. Cats refers to this as “syntax” for the type class:
We use interface syntax by importing it alongside the instances for the types we need:
Again, the compiler searches for candidates for the implicit parameters and fills them in for us:
What are Implicits?
Working with type classes in Scala means working with implicit values and implicit parameters. There are a few rules we need to know to do this effectively.
Any definitions marked implicit in Scala must be placed inside an object or trait rather than at the top level.
In the example above we packaged our type class instances in an object called JsonWriterInstances. We could equally have placed them in a companion object to JsonWriter. Placing instances in a companion object to the type class has special significance in Scala because it plays into something called implicit scope.
What is Implicit Scope?
As we saw above, the compiler searches for candidate type class instances by type.
For example, in the following expression it will look for an instance of type JsonWriter[String]:
The places where the compiler searches for candidate instances is known as the implicit scope. The implicit scope applies at the call site that is the point where we call a method with an implicit parameter.
In this article, we had a first look at type classes.
We saw the components that make up a type class:
- A trait, which is the type class.
- Type class instances, which are implicit values.
- Type class usage, which uses implicit parameters.
You can download the learning template Scala Cats - Basics demonstrating the code implementations provided in this article.
The best two Scala Cats resources I know are here:
Opinions expressed by DZone contributors are their own.