Previous

Next

Tour of Scala

Case Classes

Case classes are like regular classes with a few key differences which we will go over. Case classes are good for modeling immutable data. In the next step of the tour, we'll see how they are useful in pattern matching.

Defining a case class

A minimal case class requires the keywords case class, an identifier, and a parameter list (which may be empty):

In [1]:
case class Book(isbn: String)

val frankenstein = Book("978-0486282114")
Out[1]:
defined class Book
frankenstein: Book = Book("978-0486282114")

Notice how the keyword new was not used to instantiate the Book case class. This is because case classes have an apply method by default which takes care of object construction.

When you create a case class with parameters, the parameters are public vals.

In [2]:
case class Message(sender: String, recipient: String, body: String)
val message1 = Message("[email protected]", "[email protected]", "Ça va ?")

println(message1.sender)  // prints [email protected]
message1.sender = "[email protected]"  // this line does not compile
cmd1.sc:5: reassignment to val
val res1_3 = message1.sender = "[email protected]"  // this line does not compile
                             ^Compilation Failed
Compilation Failed

You can't reassign message1.sender because it is a val (i.e. immutable). It is possible to use vars in case classes but this is discouraged.

Comparison

Case classes are compared by structure and not by reference:

In [2]:
case class Message(sender: String, recipient: String, body: String)

val message2 = Message("[email protected]", "[email protected]", "Com va?")
val message3 = Message("[email protected]", "[email protected]", "Com va?")
val messagesAreTheSame = message2 == message3  // true
Out[2]:
defined class Message
message2: Message = Message(
  "[email protected]",
  "[email protected]",
  "Com va?"
)
message3: Message = Message(
  "[email protected]",
  "[email protected]",
  "Com va?"
)
messagesAreTheSame: Boolean = true

Even though message2 and message3 refer to different objects, the value of each object is equal.

Copying

You can create a (shallow) copy of an instance of a case class simply by using the copy method. You can optionally change the constructor arguments.

In [3]:
case class Message(sender: String, recipient: String, body: String)
val message4 = Message("[email protected]", "[email protected]", "Me zo o komz gant ma amezeg")
val message5 = message4.copy(sender = message4.recipient, recipient = "[email protected]")
message5.sender  // [email protected]
message5.recipient // [email protected]
message5.body  // "Me zo o komz gant ma amezeg"
Out[3]:
defined class Message
message4: Message = Message(
  "[email protected]",
  "[email protected]",
  "Me zo o komz gant ma amezeg"
)
message5: Message = Message(
  "[email protected]",
  "[email protected]",
  "Me zo o komz gant ma amezeg"
)
res2_3: String = "[email protected]"
res2_4: String = "[email protected]"
res2_5: String = "Me zo o komz gant ma amezeg"

The recipient of message4 is used as the sender of message5 but the body of message4 was copied directly.

Previous

Next

Tour of Scala