September 2018
Intermediate to advanced
398 pages
9h 43m
English
In Scala, we define most data structures using case classes. case class has one to many immutable attributes and provides several built-in functions compared to a standard class.
Type the following into the worksheet:
case class Person(name: String, age: Int)val mikaelNew = new Person("Mikael", 41)// 'new' is optionalval mikael = Person("Mikael", 41)// == compares values, not referencesmikael == mikaelNew// == is exactly the same as .equalsmikael.equals(mikaelNew) val name = mikael.name // a case class is immutable. The line below does not compile://mikael.name = "Nicolas"// you need to create a new instance using copyval nicolas = mikael.copy(name = "Nicolas")
In the preceding code, the text following // is a comment that explains ...
Read now
Unlock full access