O'Reilly logo

Scala Puzzlers by Nermin Šerifovic, Andrew Phillips

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Puzzler 10

A Case of Equality

Scala's case classes are an easy way to represent entities, with factory methods, extractors, and several convenience methods implemented "for free":

  class Country(val isoCode: Stringval name: String)
  case class CountryCC(isoCode: String, name: String)
    val homeOfScala = new Country("CH""Switzerland")
  val homeOfScalaCC =
    CountryCC("CH""Switzerland"// factory method
    scala> println(homeOfScala equals 
           new Country("CH""Switzerland"))
  false
    scala> println(homeOfScalaCC equals
           CountryCC("CH""Switzerland"))
  true
    scala> println(homeOfScala.toString)
  $line348.$read$$iw$$iw$Country@39eb8ede
    scala> println(homeOfScalaCC.toString)
  CountryCC(CH,Switzerland)

To give you a better idea of what's ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required