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: docs/_spec/08-pattern-matching.md
+52-1Lines changed: 52 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -179,6 +179,56 @@ The pattern matches all objects created from constructor invocations ´c(v_1, ..
179
179
A special case arises when ´c´'s formal parameter types end in a repeated parameter.
180
180
This is further discussed [here](#pattern-sequences).
181
181
182
+
### Record Patterns
183
+
184
+
```ebnf
185
+
SimplePattern ::= StableId ‘(’ [Patterns] ‘)’
186
+
```
187
+
188
+
A _record pattern_ ´r(p_1, ..., p_n)´ where ´n \geq 0´ is of the same syntactic form as a constructor pattern.
189
+
However, instead of a case class, the stable identifier ´r´ denotes a class defined in Java as a `record`, with ´n´ components of types ´T_1, ..., T_n´.
190
+
A record pattern applies only if ´r´ does not define an `unapply` or `unapplySeq` method; otherwise the pattern is an [extractor pattern](#extractor-patterns).
191
+
192
+
If the record class is monomorphic, then it must conform to the expected type of the pattern, and the component types ´T_1, ..., T_n´ are taken as the expected types of the element patterns ´p_1, ..., p_n´.
193
+
If the record class is polymorphic, then its type parameters are instantiated so that the instantiation of ´r´ conforms to the expected type of the pattern.
194
+
The instantiated component types are then taken as the expected types of the element patterns ´p_1, ..., p_n´.
195
+
The pattern matches all instances ´v´ of the record class where each element pattern ´p_i´ matches the corresponding component of ´v´.
196
+
197
+
###### Example
198
+
199
+
Given the Java records
200
+
201
+
```java
202
+
public record Rec1(int x, String y) {}
203
+
public record Rec2<T>(int x, T y) {}
204
+
```
205
+
206
+
both patterns below are record patterns:
207
+
208
+
```scala
209
+
(r2: Rec1) match {
210
+
caseRec1(i, s) => ... // i: Int, s: String
211
+
}
212
+
(r3: Rec2[String]) match {
213
+
caseRec2(i, s) => ... // i: Int, s: String
214
+
}
215
+
```
216
+
217
+
The pattern `Rec1(i, s)` matches any `Rec1`, binding `i` and `s` to its two components.
218
+
For the polymorphic `Rec2`, the type parameter `T` is instantiated to `String` so that the pattern conforms to the expected type `Rec2[String]`; hence `s` is typed as `String`.
219
+
220
+
###### Example
221
+
222
+
If a record additionally declares an extractor, the extractor takes precedence:
Here `RecUnapply(i, s)` is an extractor pattern using the declared `unapply` method, rather than a record pattern.
231
+
182
232
### Tuple Patterns
183
233
184
234
```ebnf
@@ -327,6 +377,7 @@ A pattern ´p´ is _irrefutable_ for a type ´T´, if one of the following appli
327
377
1. ´p´ is a variable pattern,
328
378
1. ´p´ is a typed pattern ´x: T'´, and ´T <: T'´,
329
379
1. ´p´ is a constructor pattern ´c(p_1, ..., p_n)´, the type ´T´ is an instance of class ´c´, the [primary constructor](05-classes-and-objects.html#class-definitions) of type ´T´ has argument types ´T_1, ..., T_n´, and each ´p_i´ is irrefutable for ´T_i´.
380
+
1. ´p´ is a record pattern ´r(p_1, ..., p_n)´, the type ´T´ is an instance of the record class ´r´ with component types ´T_1, ..., T_n´, and each ´p_i´ is irrefutable for ´T_i´.
330
381
1. ´p´ is an extractor pattern for which the extractor type is `Some[´T´]` for some type ´T´
331
382
1. ´p´ is an extractor pattern for which the extractor types `isEmpty` method is the singleton type `false`
332
383
1. ´p´ is an extractor pattern for which the return type is the singleton type `true`
@@ -538,7 +589,7 @@ In the interest of efficiency the evaluation of a pattern matching expression ma
538
589
This might affect evaluation through side effects in guards.
539
590
However, it is guaranteed that a guard expression is evaluated only if the pattern it guards matches.
540
591
541
-
If the selector of a pattern match is an instance of a [`sealed` class](05-classes-and-objects.html#modifiers), a [union type](03-types#union-and-intersection-types), or a combination thereof, the compilation of pattern matching can emit warnings which diagnose that a given set of patterns is not exhaustive, i.e. that there is a possibility of a `MatchError` being raised at run-time.
592
+
If the selector of a pattern match is an instance of a [`sealed` class](05-classes-and-objects.html#modifiers), a [union type](03-types#union-and-intersection-types), a [Java record](#record-patterns), or a combination thereof, the compilation of pattern matching can emit warnings which diagnose that a given set of patterns is not exhaustive, i.e. that there is a possibility of a `MatchError` being raised at run-time.
0 commit comments