MOCKSTACKS
EN
Questions And Answers

More Tutorials









Scala Case Classes

Examples

Case Class Equality


One feature provided for free by case classes is an auto-generated equals method that checks the value equality of all individual member fields instead of just checking the reference equality of the objects.

With ordinary classes:

class Foo(val i: Int)
val a = new Foo(3)
val b = new Foo(3)
println(a == b)// "false" because they are different objects

With case classes:

case class Foo(i: Int)
val a = Foo(3)
val b = Foo(3)
println(a == b)// "true" because their members have the same value

Generated Code Artifacts


The case modifier causes the Scala compiler to automatically generate common boilerplate code for the class. Implementing this code manually is tedious and a source of errors. The following case class definition:

case class Person(name: String, age: Int)

... will have the following code automatically generated:

class Person(val name: String, val age: Int)
 extends Product with Serializable
{
 def copy(name: String = this.name, age: Int = this.age): Person =
 new Person(name, age)
 def productArity: Int = 2
def productElement(i: Int): Any = i match {
 case 0 => name
 case 1 => age
 case _ => throw new IndexOutOfBoundsException(i.toString)
 }
 def productIterator: Iterator[Any] =
 scala.runtime.ScalaRunTime.typedProductIterator(this)
 def productPrefix: String = "Person"
 def canEqual(obj: Any): Boolean = obj.isInstanceOf[Person]
 override def hashCode(): Int = scala.runtime.ScalaRunTime._hashCode(this)
 override def equals(obj: Any): Boolean = this.eq(obj) || obj match {
 case that: Person => this.name == that.name && this.age == that.age
 case _ => false
 }
 override def toString: String =
 scala.runtime.ScalaRunTime._toString(this)
}

The case modifier also generates a companion object:

object Person extends AbstractFunction2[String, Int, Person] with Serializable {
 def apply(name: String, age: Int): Person = new Person(name, age)
 def unapply(p: Person): Option[(String, Int)] =
 if(p == null) None else Some((p.name, p.age))
}


Conclusion

In this page (written and validated by ) you learned about Scala Case Classes . What's Next? If you are interested in completing Scala tutorial, your next topic will be learning about: Scala Classes and Objects.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.