2. Case Classes - Concepts
Scala provides ‘Case Classes’ to do elegant comparisons in your code about the class
object types. for e.g. a case class looks like…
scala> abstract case class Animal
defined class Animal
scala> case class Dog(breed:String,name:String) extends Animal;
defined class Dog
scala> case class Cat(breed:String,name:String) extends Animal;
defined class Cat
Now Create instances……
scala> var anAnimal:Animal = new Cat(“Indonesian","Silly");
anAnimal: Animal = Animal()
scala> var anAnimal2:Animal = new Dog("Alsatian","Biscuit");
anAnimal2: Animal = Animal()
scala> anAnimal match {
| case Cat(breed,name) => println("The animal was cat with name :" + name + “ and breed:" + breed)
| case Dog(breed,name) => println("The animal was dog with name :" + name + “ and breed:" + breed)}
The animal was cat with name :Silly and breed:Indonesian
3. Case Classes - Concepts
More examples.....
scala> def anyGobbler(anyAnimal:Any) {
| anyAnimal match {
| case Dog(breed , name) => println("You gave me a Dog with value :" + anyAnimal);
| case Cat(breed, name) => println("You gave me a Cat :" + anyAnimal);
| case _ => println("Excuse me, why did you send me something " + anyAnimal);
| }}
anyGobbler: (anyAnimal: Any)Unit
scala> var myDog:Dog = new Dog(“Alsatian",“biscuit")
myDog: Dog = Animal()
scala> var myCat:Cat = new Cat("indonesian","silly")
myCat: Cat = Animal()
scala> anyGobbler(myDog);
You gave me a Dog with value :Animal()
scala> anyGobbler(myCat);
You gave me a Cat :Animal()
scala> anyGobbler("garbage");
Excuse me, why did you send me something garbage
4. Case Classes - Concepts
Accessing the members of the case classes instances….
scala> def anyGobbler2(anyAnimal:Any) {
| anyAnimal match {
| case Dog(breed,name) => println("You gave me a Dog with breed ::" + breed + " with name:" + name);
| case Cat(breed, name) => println("You gave me a Cat with breed:" + breed + " with name:" + name);
| case _ => println("Excuse me, why did you give me something else" + anyAnimal);
|}
|}
anyGobbler2: (anyAnimal: Any)Unit
scala> anyGobbler2(new Dog("Alsatian","Biscuit"));
You gave me a Dog with breed ::Alsatian with name: Biscuit
scala> anyGobbler2(new Cat("Indonesian","Silly"));
You gave me a Cat with breed:Indonesian with name :Silly
Define a new animal anyGobble2 doesn’t know…..
scala> class Mouse(type:String) extends Animal;
defined class Mouse
scala> anyGobbler2(new Mouse("JerryType"));
Excuse me, why did you give me something else Animal()
5. The apply() method
You can define an apply(….) method for any Class or an Object and it will be called anytime you append a pair of parenthesis ‘( )’
to the object of that class. Like below
scala> object Test {
| def apply(x:Int) = {
| x*5 }
|}
defined module Test
Calling it is as below …
scala> var a = Test(100)
a: Int = 500}
scala> class CTest {
| def apply(x:Int) = {
| x*5}
|}
defined class CTest
scala> var s = new CTest
s: CTest = CTest@5bbe2de2
scala> var p = s(100);
p: Int = 500
6. The unapply() method
Similarly you also define an unapply(….) method for any Class or an Object and it will be called during a case match block of
code as below…
scala> object Test {
| def unapply(x:Int):Option[Int] = {
| if (x > 5) Some(x) else None}
|}
defined module Test
scala> for(i <- 1 to 10) i match {
| case Test(a) => println(" value of a " + a)
| case _ => println("not matching")}
not matching
not matching
not matching
not matching
not matching
value of a 6
value of a 7
value of a 8
value of a 9
value of a 10
7. class Vs case class
scala> class Person(name:String) scala> case class CPerson(name:String)
defined class Person defined class CPerson
scala> val p = new Person("Tom") scala> var cp = CPerson("Jerry")
p: Person = Person@362a7b cp: CPerson = CPerson(Jerry)
scala> val p2 = new Person("Tom") scala> val n = cp.name
p2: Person = Person@2cd0a9b2 n: String = Jerry
scala> if(p == p2) true else false scala> var cp2 = CPerson("Jerry")
res12: Boolean = false cp2: CPerson = CPerson(Jerry)
scala> val m = p.name scala> if(cp == cp2) true else false
<console>:9: error: value name is not a member of res10: Boolean = true
Person scala> cp2.toString
val m = p.name res13: String = CPerson(Jerry)
scala> p2.toString
res14: java.lang.String = Person@2cd0a9b2
This means that the Scala compiler adds factory method for case classes that eliminate need to add ‘new’ keyword.
Parameter list of case class are added as fields /members automatically .
Compiler also adds implementations of methods toString, hashCode, and equals to the case classes.