Skip to content

Commit 8ed99c4

Browse files
authored
Merge pull request #1341 from dongxuwang/code-blocks-sync-2
Make zh_cn/tour/pattern_matching.md up to date
2 parents 8b35576 + 099fe2f commit 8ed99c4

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

_zh-cn/tour/pattern-matching.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ case class VoiceRecording(contactName: String, link: String) extends Notificatio
6666
```
6767
def showNotification(notification: Notification): String = {
6868
notification match {
69-
case Email(email, title, _) =>
70-
s"You got an email from $email with title: $title"
69+
case Email(sender, title, _) =>
70+
s"You got an email from $sender with title: $title"
7171
case SMS(number, message) =>
7272
s"You got an SMS from $number! Message: $message"
7373
case VoiceRecording(name, link) =>
@@ -81,15 +81,15 @@ println(showNotification(someSms)) // prints You got an SMS from 12345! Message
8181
8282
println(showNotification(someVoiceRecording)) // you received a Voice Recording from Tom! Click the link to hear it: voicerecording.org/id/123
8383
```
84-
`showNotification`函数接受一个抽象类`Notification`对象作为输入参数,然后匹配其具体类型。(也就是判断它是一个`Email``SMS`,还是`VoiceRecording`)。在`case Email(email, title, _)`中,对象的`email``title`属性在返回值中被使用,而`body`属性则被忽略,故使用`_`代替。
84+
`showNotification`函数接受一个抽象类`Notification`对象作为输入参数,然后匹配其具体类型。(也就是判断它是一个`Email``SMS`,还是`VoiceRecording`)。在`case Email(sender, title, _)`中,对象的`sender``title`属性在返回值中被使用,而`body`属性则被忽略,故使用`_`代替。
8585

8686
## 模式守卫(Pattern gaurds)
8787
为了让匹配更加具体,可以使用模式守卫,也就是在模式后面加上`if <boolean expression>`
8888
```
8989
9090
def showImportantNotification(notification: Notification, importantPeopleInfo: Seq[String]): String = {
9191
notification match {
92-
case Email(email, _, _) if importantPeopleInfo.contains(email) =>
92+
case Email(sender, _, _) if importantPeopleInfo.contains(sender) =>
9393
"You got an email from special someone!"
9494
case SMS(number, _) if importantPeopleInfo.contains(number) =>
9595
"You got an SMS from special someone!"
@@ -111,7 +111,7 @@ println(showImportantNotification(importantEmail, importantPeopleInfo))
111111
println(showImportantNotification(importantSms, importantPeopleInfo))
112112
```
113113

114-
`case Email(email, _, _) if importantPeopleInfo.contains(email)`中,除了要求`notification``Email`类型外,还需要`email`在重要人物列表`importantPeopleInfo`中,才会匹配到该模式。
114+
`case Email(sender, _, _) if importantPeopleInfo.contains(sender)`中,除了要求`notification``Email`类型外,还需要`sender`在重要人物列表`importantPeopleInfo`中,才会匹配到该模式。
115115

116116

117117
## 仅匹配类型

0 commit comments

Comments
 (0)