Getting Started with Scala Cats

Reading Time: 3 minutes

Introduction

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:

// Define a very simple JSON AST
sealed trait Json
final case class JsObject(get: Map[String, Json]) extends Json
final case class JsString(get: String) extends Json
final case class JsNumber(get: Double) extends Json
final case object JsNull extends Json
// The "serialize to JSON" behaviour is encoded in this trait
trait JsonWriter[A] {
  def write(value: A): Json
}

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:

final case class Person(name: String, email: String)

object JsonWriterInstances {
  implicit val stringWriter: JsonWriter[String] = new JsonWriter[String] {
    def write(value: String): Json = JsString(value)
  }
  implicit val personWriter: JsonWriter[Person] = new JsonWriter[Person] {
    def write(value: Person): Json =
      JsObject(Map(
        "name" -> JsString(value.name),
        "email" -> JsString(value.email)
      ))
  }
}

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:

object Json {
  def toJson[A](value: A)(implicit w: JsonWriter[A]): Json = w.write(value)
}

To use this object, we import any type class instances we care about and call the relevant method:

import JsonWriterInstances._
Json.toJson(Person("Dave", "dave@example.com"))

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:

Json.toJson(Person("Dave", "dave@example.com"))(personWriter)

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:

object JsonSyntax {
  implicit class JsonWriterOps[A](value: A) {
    def toJson(implicit w: JsonWriter[A]): Json = w.write(value)
  }
}

We use interface syntax by importing it alongside the instances for the types we need:

import JsonWriterInstances._
import JsonSyntax._
Person("Dave", "dave@example.com").toJson

Again, the compiler searches for candidates for the implicit parameters and fills them in for us:

Person("Dave", "dave@example.com").toJson(personWriter)

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

Json.toJson("A 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.

Summary

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.

References

The best two Scala Cats resources I know are here:

Written by 

A passionate Software Developer, recognized as a good team player, dedicated, responsible professional and a technology enthusiast. She has good time management skills. She believes in standard coding practices. Her focus always remains on creating engaging products and user experiences. She is also fond of writing tech blogs and have contributed to various blogging sites. She has also contributed to various Akka and Scala based templates.

Leave a Reply