Skip to content

Commit 8821342

Browse files
committed
add java records to pattern matching spec
1 parent fce8acb commit 8821342

1 file changed

Lines changed: 52 additions & 1 deletion

File tree

docs/_spec/08-pattern-matching.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,56 @@ The pattern matches all objects created from constructor invocations ´c(v_1, ..
179179
A special case arises when ´c´'s formal parameter types end in a repeated parameter.
180180
This is further discussed [here](#pattern-sequences).
181181

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+
case Rec1(i, s) => ... // i: Int, s: String
211+
}
212+
(r3: Rec2[String]) match {
213+
case Rec2(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:
223+
224+
```java
225+
public record RecUnapply(int i, String s) {
226+
public static RecUnapply unapply(RecUnapply r) { return new RecUnapply(1, "other"); }
227+
}
228+
```
229+
230+
Here `RecUnapply(i, s)` is an extractor pattern using the declared `unapply` method, rather than a record pattern.
231+
182232
### Tuple Patterns
183233

184234
```ebnf
@@ -327,6 +377,7 @@ A pattern ´p´ is _irrefutable_ for a type ´T´, if one of the following appli
327377
1. ´p´ is a variable pattern,
328378
1. ´p´ is a typed pattern ´x: T'´, and ´T <: T'´,
329379
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´.
330381
1. ´p´ is an extractor pattern for which the extractor type is `Some[´T´]` for some type ´T´
331382
1. ´p´ is an extractor pattern for which the extractor types `isEmpty` method is the singleton type `false`
332383
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
538589
This might affect evaluation through side effects in guards.
539590
However, it is guaranteed that a guard expression is evaluated only if the pattern it guards matches.
540591

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.
542593

543594
###### Example
544595

0 commit comments

Comments
 (0)