scala oops immutability

  • Case classes are immutable data structure.
  • Can be used with match.
case class Person(name: String)
 
val person = Person("Nitin")

By default the visibility identifier is val which makes members immutable.

Destructuring object

  • case classes can be destructured using pattern matching like shown below.
person match
    case Person(name) => println("Name : " + name)

This destructuring happens because of unapply method case provides.

This note talks about apply and unapply methods.