Making a Case for Scala's case classes
If you are like me that is fed-up with writing or generating those getters / setters in your domain objects or Data Transfer Objects or Value Objects, deferring the process of writing at compile time is definitely a boon. Scala's case classes does exactly that. Some noteworthy things to know about Scala case classes.
- They are defined with the keyword case and are always immutable in nature because the parameters are val's implicitly!
1case class Person(name: String, age: Int)
Now you could of course make the parameter as var, but be warned that your equals and hashCode behavior that you implicitly get is not bound to do what you wanted it to do
The parameters that are passed to the case classes are treated as final and are instantiated in the class constructor
Simple to instantiate! You could also define some defaults to the parameters if you do not want to supply them each and every time
1case class Person(name: String = "MyName", age: Int)
2val person = Person(29) // Would instantiate a person with name = MyName and age = 29
Gives you implicitly defined equals and hashCode implementation
You can decompose them using Scala's pattern matching feature, the reason you can pattern match a case class is because you also get the unapply method implemented for all your case classes
The getter methods are generated by default for you and if you have any var's as parameters, a setter method is also generated for that parameter that is declared as a var. But please don't try to mutate in a case class!
With all that said, let's now do a simple pattern matching snippet using a case classes:
1abstract class Animal(age: Int)
2case class Dog(name: String, age: Int) extends Animal(age)
3case class Elephant(name: String, age: Int, sex: String) extends Animal(age)
We have now defined two case classes each with its own set of properties. Here is how pattern matching could then be applied:
1def patternMatchAnimal(animal: Animal) = animal match {
2 case d: Dog => println("that was just Any Dog")
3 case d: Dog("MyDog", _) => println("that was indeed My Dog")
4 case e: Elephant(_, 100, _) => println("Wow! that elephant lived 100 years")
5 case _ => println("Were you a human! well not really")
6}
7
8patternMatchAnimal(Dog("AnyDog", 2)) // prints -> that was just Any Dog
9patternMatchAnimal(Dog("MyDog", 2)) // prints -> that was indeed My Dog
10patternMatchAnimal(Elephant("Ele", 100, "M")) // prints -> Wow! that elephant lived 100 years
11patternMatchAnimal(Elephant("Ele", 101, "M")) // prints -> Were you a human! well not really
Having said that, there is much more to case classes. Have a look at any Scala text to read more on Scala case classes!