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
Copy file name to clipboardExpand all lines: _zh-cn/tour/pattern-matching.md
+19-18Lines changed: 19 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -31,9 +31,9 @@ x match {
31
31
case _ => "many"
32
32
}
33
33
```
34
-
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_.
Case classes are especially useful for pattern matching.
50
+
案例类非常适合用于模式匹配。
51
51
52
52
```tut
53
53
abstract class Notification
@@ -80,10 +80,10 @@ println(showNotification(someSms)) // prints You got an SMS from 12345! Message
80
80
81
81
println(showNotification(someVoiceRecording)) // you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/123
82
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 `_`.
In the `case Email(email, _, _) if importantPeopleInfo.contains(email)`, the pattern is matched only if the `email` is in the list of important people.
113
+
在`case Email(email, _, _) if importantPeopleInfo.contains(email)`中,除了要求`notification`是`Email`类型外,还需要`email`在重要人物列表`importantPeopleInfo`中,才会匹配到该模式。
`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).
0 commit comments