Scala

Scala Snippet: Case Class vs plain ordinary Class

In Scala there exist the construct of a ‘case class’. According to Martin Odersky this supports you to write a “regular, non-encapsulated data structure”. It always seems to be associated with pattern matching.

So when to use a case class and when to use a ‘plain’ class?

I found this nice explanation stating:

“Case classes can be seen as plain and immutable data-holding objects that should exclusively depend on their constructor arguments.

This functional concept allows us to

  • use a compact initialisation syntax (Node(1, Leaf(2), None)))
  • decompose them using pattern matching
  • have equality comparisons implicitly defined

In combination with inheritance, case classes are used to mimic algebraic datatypes.

If an object performs stateful computations on the inside or exhibits other kinds of complex behaviour, it should be an ordinary class.

Defining a case class gives you a lot of boilerplate code for free:

  • Getters are generated for the constructor parameters. Setters are only generated when the parameters are declared as var. They are val by default.
  • A nice toString method is generated.
  • An equals and hashCode methods are generated.
  • A copy method is generated to clone an object.
  • An apply method is generated, removing the need to use the new keyword when creating a new instance of the class.
  • An unapply method is generated.

Test it out by starting the Scala repl in a terminal:

$ scala
Welcome to Scala version 2.11.4 (Java HotSpot(TM) Client VM, Java 1.7.0_75).
Type in expressions to have them evaluated.
Type :help for more information.

scala> case class Camera(brand: String, model: String)
defined class Camera

scala> val myCamera = Camera("Canon", "5D Mark III")
myCamera: Camera = Camera(Canon,5D Mark III)

scala>

If you type in the name of your object with a ‘.’ and press tab, the repl will display some of the available methods that are generated.

scala> myCamera.
asInstanceOf   brand canEqual  copy  isInstanceOf   model   productArity   productElement   productIterator   productPrefix   toString

Final note

Alvin Alexander writes that case classes are primarily intended to create “immutable records” that you can easily use in pattern matching expressions.

So while you can define the constructor parameters as var, they are intended to be val.

Reference: Scala Snippet: Case Class vs plain ordinary Class from our JCG partner Arthur Arts at the JDriven blog.

Arthur Arts

Arthur Arts is an experienced Enterprise Java software engineer and hands-on Agile Coach and Scrum Master. He has a strong focus on agile engineering principles and practices and loves to share his knowledge and learn from others. He has his own company Coded Value, is a passionate photographer and writes for agilearts.nl.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button