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: _tour/pattern-matching.md
+45-19Lines changed: 45 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -134,7 +134,7 @@ println(showNotification(someVoiceRecording)) // prints You received a Voice Re
134
134
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(sender, title, _)` the fields `sender` and `title` are used in the return value but the `body` field is ignored with `_`.
135
135
136
136
## Pattern guards
137
-
Pattern guards are simply boolean expressions which are used to make cases more specific. Just add `if <boolean expression>` after the pattern.
137
+
Pattern guards are boolean expressions which are used to make cases more specific. Just add `if <boolean expression>` after the pattern.
`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).
233
233
234
-
## Sealed classes
235
-
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.
234
+
## Sealed types
235
+
236
+
You may have noticed that in the examples above the base types are qualified
237
+
with the keyword `sealed`. This provides extra safety because the compiler
238
+
checks that the `cases` of a `match` expression are exhaustive when the base
239
+
type is `sealed`.
240
+
241
+
For instance, in the method `showNotification` defined above, if we forget
242
+
one case, say, `VoiceRecording`, the compiler emits a warning:
0 commit comments