Skip to content
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

Use p* for vararg splices #11240

Merged
merged 7 commits into from
Feb 3, 2021
Merged

Use p* for vararg splices #11240

merged 7 commits into from
Feb 3, 2021

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Jan 28, 2021

From the doc page:

The syntax of vararg splices in patterns and function arguments has changed. The new syntax uses a postfix *, analogously to how a vararg parameter is declared.

val arr = Array(1, 2, 3)
val lst = List(0, arr*)                  // vararg splice argument
lst match
   case List(0, 1, xs*) => println(xs)   // binds xs to Seq(2, 3)
   case List(1, _*) =>                   // wildcard pattern

The old syntax for splice arguments will be phased out.

/*!*/ val lst = List(0, arr: _*)         // syntax error
      lst match
         case List(1, 2, xs @ _*)        // ok, equivalent to `xs*`

Syntax

ArgumentPatterns  ::=  ‘(’ [Patterns] ‘)’
                    |  ‘(’ [Patterns ‘,’] Pattern2 ‘*’ ‘)’

ParArgumentExprs  ::=  ‘(’ [‘using’] ExprsInParens ‘)’
                    |  ‘(’ [ExprsInParens ‘,’] PostfixExpr ‘*’ ‘)’

Compatibility considerations

To enable cross compilation between Scala 2 and Scala 3, the compiler will
accept both the old and the new syntax. Under the -source 3.1 setting, an error
will be emitted when the old syntax is encountered.

Fixes #11233

@odersky odersky changed the title Use p* for vararg patterns Use p* for vararg splices Jan 28, 2021
@odersky odersky marked this pull request as ready for review January 28, 2021 17:35
@odersky
Copy link
Contributor Author

odersky commented Feb 2, 2021

@liufengyun Can you do a technical review for this?

@odersky odersky removed their assignment Feb 2, 2021
Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

compiler/src/dotty/tools/dotc/parsing/Parsers.scala Outdated Show resolved Hide resolved
@@ -297,7 +297,7 @@ PatVar ::= varid
| ‘_’
Patterns ::= Pattern {‘,’ Pattern}
ArgumentPatterns ::= ‘(’ [Patterns] ‘)’ Apply(fn, pats)
| ‘(’ [Patterns ‘,’] Pattern2 ‘:’ ‘_’ ‘*’ ‘)’
| ‘(’ [Patterns ‘,’] Pattern2 ‘*’ ‘)’
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This syntax deviates from Scala 2.

I think it would be good to change it to:

Suggested change
| ‘(’ [Patterns ‘,’] Pattern2 ‘*’ ‘)’
| ‘(’ [Patterns ‘,’] id ‘*’ ‘)’

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's orthogonal to this PR. I am not sure about that. It's the same as for other ascriptions in patterns. I think they are useful to have.


```
ArgumentPatterns ::= ‘(’ [Patterns] ‘)’
| ‘(’ [Patterns ‘,’] Pattern2 ‘*’ ‘)’
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| ‘(’ [Patterns ‘,’] Pattern2 ‘*’ ‘)’
| ‘(’ [Patterns ‘,’] id ‘*’ ‘)’

@liufengyun liufengyun assigned odersky and unassigned liufengyun Feb 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace : _* with *
3 participants