Scala case of case classes

385 views

Published on

This short tutorial contains code that illustrates case classes and their usage in Scala code.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
385
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Scala case of case classes

  1. 1. Scala – Case of Case Classes By VulcanMinds
  2. 2. Case Classes - ConceptsScala provides ‘Case Classes’ to do elegant comparisons in your code about the classobject types. for e.g. a case class looks like…scala> abstract case class Animaldefined class Animalscala> case class Dog(breed:String,name:String) extends Animal;defined class Dogscala> case class Cat(breed:String,name:String) extends Animal;defined class CatNow 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. 3. Case Classes - ConceptsMore 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)Unitscala> 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. 4. Case Classes - ConceptsAccessing 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)Unitscala> anyGobbler2(new Dog("Alsatian","Biscuit"));You gave me a Dog with breed ::Alsatian with name: Biscuitscala> anyGobbler2(new Cat("Indonesian","Silly"));You gave me a Cat with breed:Indonesian with name :SillyDefine a new animal anyGobble2 doesn’t know…..scala> class Mouse(type:String) extends Animal;defined class Mousescala> anyGobbler2(new Mouse("JerryType"));Excuse me, why did you give me something else Animal()
  5. 5. The apply() methodYou 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 belowscala> object Test { | def apply(x:Int) = { | x*5 } |}defined module TestCalling it is as below …scala> var a = Test(100)a: Int = 500}scala> class CTest { | def apply(x:Int) = { | x*5} |}defined class CTestscala> var s = new CTests: CTest = CTest@5bbe2de2scala> var p = s(100);p: Int = 500
  6. 6. The unapply() methodSimilarly you also define an unapply(….) method for any Class or an Object and it will be called during a case match block ofcode as below…scala> object Test { | def unapply(x:Int):Option[Int] = { | if (x > 5) Some(x) else None} |}defined module Testscala> for(i <- 1 to 10) i match { | case Test(a) => println(" value of a " + a) | case _ => println("not matching")}not matchingnot matchingnot matchingnot matchingnot matching value of a 6 value of a 7 value of a 8 value of a 9 value of a 10
  7. 7. class Vs case classscala> class Person(name:String) scala> case class CPerson(name:String)defined class Person defined class CPersonscala> 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.namep2: Person = Person@2cd0a9b2 n: String = Jerryscala> 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 = truePerson scala> cp2.toString val m = p.name res13: String = CPerson(Jerry)scala> p2.toStringres14: java.lang.String = Person@2cd0a9b2This 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.

×