You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `val x` above is a random integer between 0 and 10. `x` becomes the left operand of the `match` operator and on the right is an expression with four cases. The last case `_` is a "catch all" case for any number greater than 2. Cases are also called _alternatives_.
35
+
36
+
Match expressions have a value.
37
+
```tut
38
+
def matchTest(x: Int): String = x match {
39
+
case 1 => "one"
40
+
case 2 => "two"
41
+
case _ => "many"
42
+
}
43
+
matchTest(3) // many
44
+
matchTest(1) // one
45
+
```
46
+
This match expression has a type String because all of the cases return String. Therefore, the function `matchTest` returns a String.
47
+
48
+
## Matching on case classes
49
+
50
+
Case classes are especially useful for pattern matching.
51
+
52
+
```tut
53
+
abstract class Notification
54
+
55
+
case class Email(sender: String, title: String, body: String) extends Notification
56
+
57
+
case class SMS(caller: String, message: String) extends Notification
58
+
59
+
case class VoiceRecording(contactName: String, link: String) extends Notification
60
+
61
+
62
+
```
63
+
`Notification` is an abstract super class which has three concrete Notification types implemented with case classes `Email`, `SMS`, and `VoiceRecording`. Now we can do pattern matching on these case classes:
s"You got an email from $email with title: $title"
70
+
case SMS(number, message) =>
71
+
s"You got an SMS from $number! Message: $message"
72
+
case VoiceRecording(name, link) =>
73
+
s"you received a Voice Recording from $name! Click the link to hear it: $link"
74
+
}
75
+
}
76
+
val someSms = SMS("12345", "Are you there?")
77
+
val someVoiceRecording = VoiceRecording("Tom", "voicerecording.org/id/123")
78
+
79
+
println(showNotification(someSms)) // prints You got an SMS from 12345! Message: Are you there?
80
+
81
+
println(showNotification(someVoiceRecording)) // you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/123
82
+
```
83
+
The function `showNotification` takes as a parameter the abstract type `Notification` and matches on the type of `Notification` (i.e. it figures out whether it's an `Email`, `SMS`, or `VoiceRecording`). In the `case Email(email, title, _)` the fields `email` and `title` are used in the return value but the `body` field is ignored with `_`.
84
+
85
+
## Pattern guards
86
+
Pattern guards are simply boolean expressions which are used to make cases more specific. Just add `if <boolean expression>` after the pattern.
In the `case Email(email, _, _) if importantPeopleInfo.contains(email)`, the pattern is matched only if the `email` is in the list of important people.
114
+
115
+
## Matching on type only
116
+
You can match on the type like so:
117
+
```tut
118
+
abstract class Device
119
+
case class Phone(model: String) extends Device{
120
+
def screenOff = "Turning screen off"
121
+
}
122
+
case class Computer(model: String) extends Device {
123
+
def screenSaverOn = "Turning screen saver on..."
124
+
}
125
+
126
+
def goIdle(device: Device) = device match {
127
+
case p: Phone => p.screenOff
128
+
case c: Computer => c.screenSaverOn
129
+
}
130
+
```
131
+
`def goIdle` has a different behavior depending on the type of `Device`. This is useful when the case needs to call a method on the pattern. It is a convention to use the first letter of the type as the case identifier (`p` and `c` in this case).
132
+
133
+
## Sealed classes
134
+
Traits and classes can be marked `sealed` which means all subtypes must be declared in the same file. This assures that all subtypes are known.
135
+
136
+
```tut
137
+
sealed abstract class Furniture
138
+
case class Couch() extends Furniture
139
+
case class Chair() extends Furniture
140
+
141
+
def findPlaceToSit(piece: Furniture): String = piece match {
142
+
case a: Couch => "Lie on the couch"
143
+
case b: Chair => "Sit on the chair"
144
+
}
145
+
```
146
+
This is useful for pattern matching because we don't need a "catch all" case.
147
+
148
+
## Notes
149
+
150
+
Scala's pattern matching statement is most useful for matching on algebraic types expressed via [case classes](case-classes.html).
151
+
Scala also allows the definition of patterns independently of case classes, using `unapply` methods in [extractor objects](extractor-objects.html).
0 commit comments