Lesson 19 Case Classes to structure your data
After reading this lesson, you will be able to:
- Represent immutable structured data using case classes
- Decide when to use case objects rather than regular objects
- Use case classes together with a pattern matching statement
In the previous unit, you have mastered how to create a simple HTTP server. In this lesson, you’ll discover an essential tool in your Scala’s toolbox, called “case class”. When coding, dealing with data is a fundamental and recurring task. A case class provides a convenient and efficient way to represent your data in an immutable way: this allows you to share data between multiple threads safely. Being able to express data efficiently and conveniently is essential to make sure our program works correctly. You will also learn about case objects and how they can be useful when serialization is involved. Finally, you will see how pattern matching provides dedicated support for case classes thanks to the unapply function. In the capstone, you’ll use case classes and case objects to represent the core elements of the game “Paper, Rock, Scissors, Lizard, Spock!”.