-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Match if sub cases #23786
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
Match if sub cases #23786
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0a391cd
Add SubMatch Tree
EugeneFlesselle 17ca7fc
Add scala.language.experimental.matchWithSubCases
EugeneFlesselle 3922789
Add parsing of match sub cases
EugeneFlesselle 4c28954
Add pickling of match sub cases
EugeneFlesselle 5e46b01
Handle sub cases in PatternMatcher
EugeneFlesselle e26be67
Handle exhaustivity and reachability checking of matches with sub cases
EugeneFlesselle 352317c
Handle inlining of matches with sub cases
EugeneFlesselle 3e89455
Test match with sub sub cases and catch with sub cases
EugeneFlesselle 3b86dc9
Handle partial functions with sub cases
EugeneFlesselle 4da6103
Rename experimental feature to `subCases`
EugeneFlesselle f7e0512
Allow single sub case without new line
EugeneFlesselle 367acdd
Replace sub match introducer keyword from `with` to `if`
EugeneFlesselle 24bdb6e
Fix error case EmptyTree span
EugeneFlesselle e5a73b0
Use preexisting tag for pickling of match with sub cases
EugeneFlesselle 466da83
Add reference doc
EugeneFlesselle c432c0f
Apply suggestions from code review
hamzaremmal 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
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
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
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,72 @@ | ||
--- | ||
layout: doc-page | ||
title: "Match Expressions with Sub Cases" | ||
--- | ||
|
||
A case in a match expression can be followed by another sub-match expression, introduced by the `if` keyword. | ||
For example: | ||
|
||
```scala | ||
enum Version: | ||
case Legacy | ||
case Stable(major: Int, minor: Int) | ||
|
||
case class Document(title: String, version: Version) | ||
|
||
def version(d: Option[Document]) = d match | ||
case Some(x) if x.version match | ||
case Version.Stable(m, n) if m > 2 => s"$m.$n" | ||
case Version.Legacy => "legacy" | ||
case _ => "unsupported" | ||
|
||
assert(version(Some(Document("...", Version.Stable(3, 1)))) = "3.1") | ||
assert(version(Some(Document("...", Version.Stable(2, 1)))) = "unsupported") | ||
assert(version(Some(Document("...", Version.Legacy))) = "legacy") | ||
assert(version(Some(Document("...", None))) = "unsupported") | ||
``` | ||
|
||
The cases of a sub-match expression are tested iff the outer case matches. | ||
The sub match scrutinee can refer to variables bound from the outer pattern. | ||
Evaluation of sub-matches then proceeds as usual. | ||
For example, if `version` is applied on `Some(Document("...", Version.Stable(3, 1)))`, the first outer pattern matches (i.e., `Some(x)`), causing the sub match expression to be evaluated with scrutinee `Version.Stable(3, 1)`, yielding `"3.1"`. | ||
|
||
The cases of a sub-match expression need not be exhaustive. | ||
If they were, we would not need sub-match at all: a usual match in the body of the first case would suffice, | ||
e.g., `case Some(x) => x.version match ...`. | ||
If none of the sub-cases succeed, then control flow returns to the outer match expression and proceeds as though the current case had not matched. | ||
For example, `Some(Document("...", Version.Stable(2, 1)))` matches the first pattern, but none of its sub-cases, and we therefore obtain the result `"unsupported"`. | ||
|
||
More generally, sub-matches also allow: | ||
- Arbitrary nesting, e.g. sub-sub-matches are supported. | ||
- Interleaved boolean guards, e.g. `case Some(x: Int) if x != 0 if x match ...`. | ||
- Interleaving pattern extractors and computations for the scrutinees of sub-matches. | ||
|
||
|
||
## Motivation | ||
|
||
Without sub matches, one would typically duplicate either the default case or the outer pattern. | ||
That is, use: | ||
```scala | ||
def version(d: Option[Document]) = d match | ||
case Some(x) => x.version match | ||
case Version.Stable(m, n) if m > 2 => s"$m.$n" | ||
case Version.Legacy => "legacy" | ||
case _ => "unsupported" | ||
case _ => "unsupported" | ||
``` | ||
or | ||
```scala | ||
def version(d: Option[Document]) = d match | ||
case Some(Document(_, Version.Stable(m, n))) if m > 2 => s"$m.$n" | ||
case Some(Document(_, Version.Legacy)) => "legacy" | ||
case _ => "unsupported" | ||
``` | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.