extending case classes
While Scala case class
es are best used to represent Data objects, it can be handy to extend their behavior beyond the standards set in the Scala language.
For example, a “composite getter”, #fullName
that combines two constructor parameters, firstName
and lastName
:
trait Person {
val firstName: String
val lastName: String
def fullName: String = s"$firstName $lastName"
}
case class Student(firstName: String, lastName: String) extends Person
val billy = Student("billy", "ocean")
billy.fullName
// res3: String = "billy ocean"