Alessandro Lacava

on Designing and Developing Software. In love with Functional Programming.

Scala Case Classes In Depth

For this post I’ll consider the following simple case class unless otherwise specified:

1
case class Person(lastname: String, firstname: String, birthYear: Int)

Common knowledge about case classes

When you declare a case class the Scala compiler does the following for you:

  • Creates a class and its companion object.
  • Implements the apply method that you can use as a factory. This lets you create instances of the class without the new keyword. E.g.:
1
2
3
4
val p = Person("Lacava", "Alessandro", 1976)

// instead if the slightly more verbose:
val p = new Person("Lacava", "Alessandro", 1976)
  • Prefixes all arguments, in the parameter list, with val. This means the class is immutable, hence you get the accessors but no mutators. E.g.:
1
2
3
val lastname = p.lastname
// the following won't compile:
p.lastname = "Brown"
  • Adds natural implementations of hashCode, equals and toString. Since == in Scala always delegates to equals, this means that case class instances are always compared structurally. E.g.:
1
2
3
4
5
val p_1 = Person("Brown", "John", 1969)
val p_2 = Person("Lacava", "Alessandro", 1976)

p == p_1 // false
p == p_2 // true
  • Generates a copy method to your class to create other instances starting from another one and keeping some arguments the same. E.g.: Create another instance keeping the lastname and changing firstname and birthYear:
1
2
// the result is: Person(Lacava,Michele,1972), my brother :)
val p_3 = p.copy(firstname = "Michele", birthYear = 1972)
  • Probably, most importantly, since the compiler implements the unapply method, a case class supports pattern matching. This is especially important when you define an Algebraic Data Type (ADT). E.g.:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
sealed trait Maybe[+T]
case class Value[T](value: T) extends Maybe[T]
case object NoValue extends Maybe[Nothing]

val v: Maybe[Int] = Value(42)
val v_1: Maybe[Int] = NoValue

def logValue[T](value: Maybe[T]): Unit = value match {
  case Value(v) => println(s"We have a value here: $v")
  case NoValue => println("I'm sorry, no value")
}

logValue(v) // prints We have a value here: 42
logValue(v_1) // prints I'm sorry, no value

As you probably already know, when your class has no argument you use a case object instead of a case class with an empty parameter list.

Apart from being used in pattern matching the unapply method lets you deconstruct a case class to extract it’s fields, both during pattern matching and as a simple expression to extract some of its fields. E.g.:

1
2
3
val Person(lastname, _, _) = p

println(lastname) // prints Lacava

Not so common knowledge about case classes

  • What if you need a function that, given your case class arguments as parameters, creates an instance of the class? Here’s how you can do it by partially applying apply (no pun intended :)):
1
2
3
4
val personCreator: (String, String, Int) => Person = Person.apply _

// the result is: Person(Brown,John,1969)
personCreator("Brown", "John", 1969)
  • What if you want your function, from the previous point, to be curried? Enters the curried method:
1
2
3
4
5
6
val curriedPerson: String => String => Int => Person = Person.curried

val lacavaBuilder: String => Int => Person = curriedPerson("Lacava")

val me = lacavaBuilder("Alessandro")(1976)
val myBrother = lacavaBuilder("Michele")(1972)
  • What about obtaining a function that accepts a tuple whose arity is equal to the number of the case class arguments, and produces an instance of the class? Well, there’s the tupled method for that:
1
2
3
4
5
val tupledPerson: ((String, String, Int)) => Person = Person.tupled

val meAsTuple: (String, String, Int) = ("Lacava", "Alessandro", 1976)

val meAsPersonAgain: Person = tupledPerson(meAsTuple)
  • You could also need a function that, given an instance of your class as input, produces an Option[TupleN[A1, A2, ..., AN]] as output, where N is the number of the case class arguments and A1, A2, ..., AN are their types. E.g.:
1
2
3
val toOptionOfTuple: Person => Option[(String, String, Int)] = Person.unapply _

val x: Option[(String, String, Int)] = toOptionOfTuple(p) // Some((Lacava,Alessandro,1976))

The curried and tupled methods are inherited from AbstractFunctionN which is extended by the autogenerated companion object. N is the number of the case class formal parameters. Note that, of course, if N = 1 you won’t get curried and tupled because they wouldn’t make sense for just one parameter!

Defining a case class using the curried form

There’s another less-known way of defining a case class, e.g.:

1
case class Keyword(text: String)(source: String, foo: Int)

The formal parameters in the first parameter section of a case class (just text in this case) are called elements; they are treated specially. All the goodies you get when you define a case class (accessors, pattern matching support, copy method, …) only apply to the first section. For example you don’t have an accessor for source since the compiler didn’t implicitly prefix it with val, like it did for text instead. E.g.:

1
2
3
4
val k1 = Keyword("restaurant")("storage", 1)

// won't compile
val source = k1.source

You can solve the accessor problem by prefixing the parameters with val. E.g.:

1
case class Keyword(text: String)(val source: String, val foo: Int)

Anyway you still won’t get all the other case class features. For instance, you cannot use the copy method by specifying only the source parameter. You have to specify, at least, all the parameters of the sections successive to the first. E.g.:

1
2
3
4
5
// won't compile
val k2 = k1.copy()(source = "web")

// will compile
val k3 = k1.copy()(source = "web", foo = 1)

Finally, the companion object of a case class defined in such a way won’t extend AbstractFunctionN, so the tupled and curried methods are not available.

At this point the natural question that may arise is: “Why on earth should I want to define a case class in such a way?” Apparently there are cases when it could be a reasonable choice. For example suppose that, for your business model, two instances of Keyword are to be considered equal iff they have the same text field. Well, in such a case by defining the case class using the curried form you’ll get what you want. E.g.:

1
2
3
4
val k1 = Keyword("restaurant")("storage", 1)
val k2 = Keyword("restaurant")("web", 2)

k1 == k2 // true!

That’s because also the equals implementation, you get for free for case classes, only applies to the first parameter section, so only to text in this case. I’m not saying here that this is always the best choice but it could be of help in certain situations.

In fact, you could define your case class as usual and override equals on your own. However overriding equals is not very trivial. Indeed, before doing that I recommend you read the chapter 30 of Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition - Odersky, Spoon, Venners. Its title is Object Equality and it’s just 25 pages long!

For the most curious ones

Furthermore, since each case class extends the Product trait it inherits the following methods:

  • def productArity: Int, returns the size of this product. In this case it corresponds to the number of arguments in the case class. E.g.:
1
2
3
val p = Person("Lacava", "Alessandro", 1976)

val arity = p.productArity // equals to 3
  • def productElement(n: Int): Any, returns the n-th element of this product, 0-based. In this case it corresponds to the n-th argument of the class. E.g.:
1
val lastname: Any = p.productElement(0) // Lacava
  • def productIterator: Iterator[Any], returns an iterator over all the elements of this product which, in the case class context, they are its arguments.

  • def productPrefix: String, returns a string used in the toString method of the derived classes. In this case it’s the name of the class. E.g.:

1
val className: String = p.productPrefix // the result is Person

Final Notes

  • I used type declarations in many expressions just to make things clearer. Of course I could have left them out and let the type inferer do its job.

  • Some Product’s methods return Any-based types, namely productElement and productIterator. For example, p.productElement(0) returns the lastname but it is of type Any so if you need to use it as String you have to cast it, which is an operation you should strive to avoid as much as possible.

  • Product extends Equals so every case class also inherits the canEqual method but, of course, going into its details is not the scope of this post. Besides, you don’t have to worry about it because it’s used interally by the autogenerated equals method, unless you decide to implement your own version of equals in which case you need to take into account canEqual. Again, in such a case I strongly suggest you read the chapter cited here.

Comments