-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Change match syntax #7610
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
Change match syntax #7610
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
14e8847
Drop special single-case syntax for match
odersky 02617be
Change match syntax
odersky a5d640e
Drop single case match in test
odersky 92a6b25
Fix indentation in test
odersky c764daf
Drop single case match in another test
odersky 1a2cf42
Revert to previous syntax for inline match and inline if
odersky 09e5aec
Add reference doc page
odersky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
layout: doc-page | ||
title: Match Expressions | ||
--- | ||
|
||
The syntactical precedence of match expressions has been changed. | ||
`match` is still a keyword, but it is used like an alphabetical operator. This has several consequences: | ||
|
||
1. `match` expressions can be chained: | ||
|
||
```scala | ||
xs match { | ||
case Nil => "empty" | ||
case x :: xs1 => "nonempty" | ||
} match { | ||
case "empty" => 0 | ||
case "nonempty" => 1 | ||
} | ||
|
||
2. `match` may follow a period: | ||
|
||
```scala | ||
if xsDefined | ||
&& xs.match { | ||
case Nil => false | ||
case _ => true | ||
} | ||
then "nonempty" | ||
else "empty" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fear the selection may break the regularity of Scala syntax. It seems the use case is not important enough to compensate for the loss of regularity. |
||
|
||
3. The scrutinee of a match expression must be an `InfixExpr`. Previously the scrutinee could be followed by a type ascription `: T`, but this is no longer supported. So `x : T match { ... }` now has to be | ||
written `(x: T) match { ... }`. | ||
|
||
## Syntax | ||
|
||
The new syntax of match expressions is as follows. | ||
``` | ||
InfixExpr ::= ... | ||
| InfixExpr MatchClause | ||
SimpleExpr ::= ... | ||
| SimpleExpr ‘.’ MatchClause | ||
MatchClause ::= ‘match’ ‘{’ CaseClauses ‘}’ | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
-- Error: tests/neg/cannot-reduce-inline-match.scala:3:13 -------------------------------------------------------------- | ||
3 | inline x match { // error | ||
| ^ | ||
| cannot reduce inline match with | ||
| scrutinee: { | ||
| "f" | ||
| } : String("f") | ||
| patterns : case _:Int | ||
| ^ | ||
| cannot reduce inline match with | ||
| scrutinee: { | ||
| "f" | ||
| } : String("f") | ||
| patterns : case _:Int | ||
| This location is in code that was inlined at cannot-reduce-inline-match.scala:9 | ||
4 | case _: Int => | ||
5 | } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
object Test { | ||
List(1: @unchecked, 2, 3): @unchecked match { | ||
(List(1: @unchecked, 2, 3): @unchecked) match { | ||
case a :: as => | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package matches | ||
object Test with | ||
2 min 3 match | ||
case 2 => "OK" | ||
case 3 => "?" | ||
match | ||
case "OK" => | ||
case "?" => assertFail() | ||
|
||
val x = 4 | ||
if 2 < 3 | ||
|| x.match | ||
case 4 => true | ||
case _ => false | ||
|| (2 + 2).match | ||
case 4 => true | ||
case _ => false | ||
then | ||
println("ok") | ||
end Test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ trait C | |
|
||
def fancy(given a: A, b: B, c: C) = "Fancy!" | ||
def foo(implicit a: A, b: B, c: C) = "foo" | ||
|
||
given A, B {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So that's why they call it "dotty".