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 FreeThis blog was first published on Knoldus Blogs
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:
xxxxxxxxxx
// 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:
xxxxxxxxxx
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:
xxxxxxxxxx
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:
xxxxxxxxxx
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:
xxxxxxxxxx
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:
xxxxxxxxxx
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:
xxxxxxxxxx
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:
xxxxxxxxxx
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]:
xxxxxxxxxx
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.
Learning Template
You can download the learning template Scala Cats - Basics demonstrating the code implementations provided in this article.
References
The best two Scala Cats resources I know are here:
- The Cats library is available at github.com/typelevel/cats
- The book, Advanced Scala with Cats, is available at underscore.io/books/advanced-scala/
Opinions expressed by DZone contributors are their own.
Comments