Skip to content

More details on Vararg Patterns #5339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 35 additions & 42 deletions docs/docs/reference/changed/vararg-patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,38 @@ layout: doc-page
title: "Vararg Patterns"
---

The syntax of vararg patterns has changed. In the new syntax one
writes varargs in patterns exactly like one writes them in
expressions, using a `: _*` type annotation:

xs match {
case List(1, 2, xs: _*) => println(xs) // binds xs
case List(1, _ : _*) => // wildcard pattern
}

The old syntax, which is shorter but less regular, is no longer
supported:

/*!*/ case List(1, 2, xs @ _*) // syntax error
/*!*/ case List(1, 2, _*) => ... // syntax error

Another change relates to extractors producing values with `T*` types, which can then be
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`).

Example: Previously, this was correct:

class Person(val name: String, val children: Person *)
object Person {
def unapplySeq(p: Person) = Some((p.name, p.children))
}

def childCount(p: Person) = p match {
case Person(_, ns @ _*) => ns.length
}

Now, the equivalent program is written as follows:

class Person(val name: String, val children: Person *)
object Person {
def unapply(p: Person) = Some((p.name, p.children))
}

def childCount(p: Person) = p match {
case Person(_, ns : _*) => ns.length
}



The syntax of vararg patterns has changed. In the new syntax one writes varargs in patterns exactly
like one writes them in expressions, using a `: _*` type annotation:

```scala
xs match {
case List(1, 2, xs: _*) => println(xs) // binds xs
case List(1, _ : _*) => // wildcard pattern
}
```

The old syntax, which is shorter but less regular, is no longer supported.

```scala
/*!*/ case List(1, 2, xs @ _*) // syntax error
/*!*/ case List(1, 2, _*) => ... // syntax error
```

The change to the grammar is:

```diff
SimplePattern ::= ‘_’
| varid
| Literal
| StableId
| StableId ‘(’ [Patterns ‘)’
- | StableId ‘(’ [Patterns ‘,’] [varid ‘@’] ‘_’ ‘*’ ‘)’
+ | StableId ‘(’ [Patterns ‘,’] (varid | ‘_’) ‘:’ ‘_’ ‘*’ ‘)’
| ‘(’ [Patterns] ‘)’
| XmlPattern
```

## Compatibility considerations

Under the `-language:Scala2` option, Dotty will accept both the old and the new syntax.
A migration warning will be emitted when the old syntax is encountered.