官术网_书友最值得收藏!

Scala case classes

Scala case classes provide a convenient mechanism to work with objects that hold values. Let's look at an example in Scala REPL. The case class defined in the following code will be used in other example codes in this chapter:

scala> case class Person(fname: String, lname: String, age: Int)
defined class Person

scala> val jon = Person("Jon", "Doe", 21)
jon: Person = Person(Jon,Doe,21)

In the preceding example, we have defined a Scala case class called Person with three attributes, namely fname, lname, and age. We created an instance, jon, of the Person class without using the new keyword. Also, note that the jon object's attributes are printed out in a easy-to-use form. There are several such convenient features associated with Scala case classes that are extremely beneficial for programmers in general, particularly someone who deals with data.

Let's look at another convenient feature of Scala case classes, namely the copy objectWe'll copy a Scala case class object by updating only the fname attribute using the following code:

scala> case class Person(fname: String, lname: String, age: Int)
defined class Person

scala> val jon = Person("Jon", "Doe", 21)
jon: Person = Person(Jon,Doe,21)

scala> val jonNew = jon.copy(fname="John")
jonNew: Person = Person(John,Doe,21)

This feature comes in really handy during data processing when we work with a template representation and generate specific instances from a template by updating a subset of attributes.

Another great feature of case classes is pattern matching, which helps in writing flexible code that is easier to work with. Let's look at an example of pattern matching in Scala REPL, as shown in the following code:

scala> def isJon(p: Person) = {
| p match {
| case Person("Jon", _, _) => {println("I am Jon"); true}
| case Person(n,_,_) => {println(s"I am not Jon but I am ${n}"); false}
| case _ => {println("I am not Jon but I am something other than Person"); false}
| }
| }
isJon: (p: Person)Boolean

scala> val jon = Person("Jon", "Doe", 25)
jon: Person = Person(Jon,Doe,25)

scala> isJon(jon)
I am Jon
res13: Boolean = true

scala> val bob = Person("Bob", "Crew", 27)
bob: Person = Person(Bob,Crew,27)

scala> isJon(bob)
I am not Jon but I am Bob
res14: Boolean = false

scala> isJon(null)
I am not Jon but I am something other than Person
res16: Boolean = false

We can explore the same example in the IDE, as shown in the following screenshot:

Using the IDE, we can clearly see the properties of the case class. Another great option is to use the Scala worksheet feature in IDE to explore this example, as shown in the following screenshot:

The preceding screenshot shows a fairly simple example of pattern matching using Scala case classes that illustrates the simplicity and power of this feature. In the data analysis world, pattern matching has been found to be extremely useful in solving certain categories of problems. Scala provides an intuitive, elegant, and simple way to take advantage of pattern matching.

Let's look at the preceding example in a bit more detail, looking at the following lines:

  • Line #4: case Person("Jon",  _,  _) means any person whose first name is Jon
  • Line #7: case Person(n, _, _) means any person with the first name is extracted into variable n
  • Line #10: case _ means anything that does not match line #4 and line #7

With classic pattern matching, it is generally necessary for you to write a significant amount of boilerplate code with if-then-else types of constructs. Scala and its case classes provide a concise and expressive way to solve this problem.

主站蜘蛛池模板: 平利县| 迭部县| 德安县| 达拉特旗| 安国市| 仁化县| 白银市| 连平县| 牡丹江市| 曲松县| 阿坝县| 宁城县| 安乡县| 永昌县| 英吉沙县| 宝清县| 上林县| 仁寿县| 民丰县| 栖霞市| 丹寨县| 漳州市| 来凤县| 六盘水市| 扎囊县| 南川市| 年辖:市辖区| 左权县| 西丰县| 门源| 邯郸县| 永仁县| 壤塘县| 平顶山市| 佛山市| 双鸭山市| 上杭县| 屏东县| 玉溪市| 宿迁市| 沙湾县|