You’re working with match expressions, actors, or other situations where you want to use the case class syntax to generate boilerplate code, including accessor and mutator methods, along with apply, unapply, toString, equals, and hashCode methods, and more.
Define your class as a case class, defining any parameters it needs in its constructor
// name and relation are 'val' by default
Defining a class as a case class results in a lot of boilerplate code being generated, with the following benefits:
- An apply method is generated, so you don’t need to use the new keyword to create a new instance of the class.
- Accessor methods are generated for the constructor parameters because case class constructor parameters are val by default. Mutator methods are also generated for parameters declared as var.
- A good, default toString method is generated.
- An unapply method is generated, making it easy to use case classes in match expressions.
- equals and hashCode methods are generated.
- A copy method is generated.