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

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.

主站蜘蛛池模板: 安阳市| 南京市| 禄劝| 天全县| 万荣县| 安庆市| 泰来县| 静海县| 泾阳县| 西林县| 修武县| 麻城市| 甘孜县| 改则县| 钟山县| 衡东县| 蛟河市| 临漳县| 神木县| 洞口县| 土默特左旗| 南郑县| 佛学| 台南市| 彭州市| 太康县| 正镶白旗| 晋州市| 衡阳市| 陵川县| 揭东县| 平遥县| 惠东县| 凉山| 宣城市| 泸定县| 嘉义市| 阳原县| 柳江县| 会宁县| 佳木斯市|