Skip to content

Commit 6878fed

Browse files
committed
More details on Vararg Patterns
Changes to `unapply` are dropped and will be reverted in the future. #3248 is reopened.
1 parent c02501a commit 6878fed

File tree

1 file changed

+35
-42
lines changed

1 file changed

+35
-42
lines changed

docs/docs/reference/changed/vararg-patterns.md

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,38 @@ layout: doc-page
33
title: "Vararg Patterns"
44
---
55

6-
The syntax of vararg patterns has changed. In the new syntax one
7-
writes varargs in patterns exactly like one writes them in
8-
expressions, using a `: _*` type annotation:
9-
10-
xs match {
11-
case List(1, 2, xs: _*) => println(xs) // binds xs
12-
case List(1, _ : _*) => // wildcard pattern
13-
}
14-
15-
The old syntax, which is shorter but less regular, is no longer
16-
supported:
17-
18-
/*!*/ case List(1, 2, xs @ _*) // syntax error
19-
/*!*/ case List(1, 2, _*) => ... // syntax error
20-
21-
Another change relates to extractors producing values with `T*` types, which can then be
22-
matched by vararg patterns. Previously, such extractors used an `unapplySeq` whereas now they use an `unapply` (in the long term, we plan to get rid of `unapplySeq` altogether, replacing all its usages with `unapply`).
23-
24-
Example: Previously, this was correct:
25-
26-
class Person(val name: String, val children: Person *)
27-
object Person {
28-
def unapplySeq(p: Person) = Some((p.name, p.children))
29-
}
30-
31-
def childCount(p: Person) = p match {
32-
case Person(_, ns @ _*) => ns.length
33-
}
34-
35-
Now, the equivalent program is written as follows:
36-
37-
class Person(val name: String, val children: Person *)
38-
object Person {
39-
def unapply(p: Person) = Some((p.name, p.children))
40-
}
41-
42-
def childCount(p: Person) = p match {
43-
case Person(_, ns : _*) => ns.length
44-
}
45-
46-
47-
6+
The syntax of vararg patterns has changed. In the new syntax one writes varargs in patterns exactly
7+
like one writes them in expressions, using a `: _*` type annotation:
8+
9+
```scala
10+
xs match {
11+
case List(1, 2, xs: _*) => println(xs) // binds xs
12+
case List(1, _ : _*) => // wildcard pattern
13+
}
14+
```
15+
16+
The old syntax, which is shorter but less regular, is no longer supported.
17+
18+
```scala
19+
/*!*/ case List(1, 2, xs @ _*) // syntax error
20+
/*!*/ case List(1, 2, _*) => ... // syntax error
21+
```
22+
23+
The change to the grammar is:
24+
25+
```diff
26+
SimplePattern ::= ‘_’
27+
| varid
28+
| Literal
29+
| StableId
30+
| StableId ‘(’ [Patterns ‘)’
31+
- | StableId ‘(’ [Patterns ‘,’] [varid ‘@’] ‘_’ ‘*’ ‘)’
32+
+ | StableId ‘(’ [Patterns ‘,’] (varid | ‘_’) ‘:’ ‘_’ ‘*’ ‘)’
33+
| ‘(’ [Patterns] ‘)’
34+
| XmlPattern
35+
```
36+
37+
## Compatibility considerations
38+
39+
Under the `-language:Scala2` option, Dotty will accept both the old and the new syntax.
40+
A migration warning will be emitted when the old syntax is encountered.

0 commit comments

Comments
 (0)