-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Two fixes to NamedTuple pattern matching #22953
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
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
6 | ||
6 | ||
6 | ||
6 | ||
7 | ||
6 | ||
7 | ||
(6) |
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,26 @@ | ||
object NameBaseExtractor { | ||
def unapply(x: Int): Some[(someName: Int)] = Some((someName = x + 3)) | ||
} | ||
object NameBaseExtractor2 { | ||
def unapply(x: Int): Some[(someName: Int, age: Int)] = Some((someName = x + 3, age = x + 4)) | ||
} | ||
@main | ||
def Test = | ||
val x1 = 3 match | ||
case NameBaseExtractor(someName = x) => x | ||
println(x1) | ||
val NameBaseExtractor(someName = x2) = 3 | ||
println(x2) | ||
val NameBaseExtractor((someName = x3)) = 3 | ||
println(x3) | ||
|
||
val NameBaseExtractor2(someName = x4, age = x5) = 3 | ||
println(x4) | ||
println(x5) | ||
|
||
val NameBaseExtractor2((someName = x6, age = x7)) = 3 | ||
println(x6) | ||
println(x7) | ||
|
||
val NameBaseExtractor(y1) = 3 | ||
println(y1) |
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,3 @@ | ||
3 | ||
6 | ||
3 |
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,15 @@ | ||
case class C(someName: Int) | ||
|
||
object NameBaseExtractor3 { | ||
def unapply(x: Int): Some[C] = Some(C(someName = x + 3)) | ||
} | ||
|
||
@main | ||
def Test = { | ||
val C(someName = xx) = C(3) | ||
println(xx) | ||
val NameBaseExtractor3(C(someName = x)) = 3 | ||
println(x) | ||
C(3) match | ||
case C(someName = xx) => println(xx) | ||
} |
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,27 @@ | ||
case class CaseClass(a: Int) | ||
|
||
object ProductMatch_CaseClass { | ||
def unapply(int: Int): CaseClass = CaseClass(int) | ||
} | ||
|
||
object ProductMatch_NamedTuple { | ||
def unapply(int: Int): (a: Int) = (a = int) | ||
} | ||
|
||
object NameBasedMatch_CaseClass { | ||
def unapply(int: Int): Some[CaseClass] = Some(CaseClass(int)) | ||
} | ||
|
||
object NameBasedMatch_NamedTuple { | ||
def unapply(int: Int): Some[(a: Int)] = Some((a = int)) | ||
} | ||
|
||
object Test { | ||
val ProductMatch_CaseClass(a = x1) = 1 // ok, was pattern's type (x1 : Int) is more specialized than the right hand side expression's type Int | ||
val ProductMatch_NamedTuple(a = x2) = 2 // ok, was pattern binding uses refutable extractor `org.test.ProductMatch_NamedTuple` | ||
val NameBasedMatch_CaseClass(a = x3) = 3 // ok, was pattern's type (x3 : Int) is more specialized than the right hand side expression's type Int | ||
val NameBasedMatch_NamedTuple(a = x4) = 4 // ok, was pattern's type (x4 : Int) is more specialized than the right hand side expression's type Int | ||
|
||
val CaseClass(a = x5) = CaseClass(5) // ok, was pattern's type (x5 : Int) is more specialized than the right hand side expression's type Int | ||
val (a = x6) = (a = 6) // ok | ||
} |
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.
Implementation looks good.
It seems the tests were intended to have check files (they
println
things rather thanassert
), but there are no checkfiles.