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

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.

主站蜘蛛池模板: 陈巴尔虎旗| 左贡县| 汾西县| 木里| 浮梁县| 新安县| 吉水县| 龙海市| 三明市| 时尚| 中江县| 沙洋县| 博兴县| 论坛| 廉江市| 伊金霍洛旗| 林芝县| 调兵山市| 丁青县| 台南县| 汤阴县| 察哈| 永嘉县| 乌什县| 云霄县| 株洲县| 喀喇沁旗| 兴山县| 姜堰市| 太康县| 晋宁县| 柯坪县| 通海县| 沾益县| 淮阳县| 宜阳县| 龙山县| 竹北市| 图片| 普兰县| 资兴市|