-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Fix #4984: support name-based unapplySeq #5078
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2a5e33f
Fix #4984: support name-based unapplySeq
liufengyun 48b7dbb
use toSeq type as element type and check type
liufengyun 6999ca8
add neg test
liufengyun 4c12ed3
address review: try length if lengthCompare unavailable
liufengyun 3b93d98
add more tests
liufengyun 9304ed6
fix failed test neg/i3248.scala
liufengyun 255c5bb
update docs for name-based unapplySeq
liufengyun 94a93e5
address review
liufengyun 4d00b92
fix ci: member types should go through asSeenFrom
liufengyun 6a0fc8c
Cleanup + Documentation + More neg tests
allanrenucci 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 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 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 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 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 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,38 @@ | ||
object Array2 { | ||
def unapplySeq(x: Array[Int]): Data = new Data | ||
class Data { | ||
def isEmpty: Boolean = false | ||
def get: Data = this | ||
def lengthCompare(len: Int): Int = 0 | ||
def apply(i: Int): Int = 3 | ||
// drop return type, not conforming to apply's | ||
def drop(n: Int): scala.Seq[String] = Seq("hello") | ||
def toSeq: scala.Seq[Int] = Seq(6, 7) | ||
} | ||
} | ||
|
||
object Array3 { | ||
def unapplySeq(x: Array[Int]): Data = new Data | ||
class Data { | ||
def isEmpty: Boolean = false | ||
def get: Data = this | ||
def lengthCompare(len: Int): Int = 0 | ||
// missing apply | ||
def drop(n: Int): scala.Seq[Int] = ??? | ||
def toSeq: scala.Seq[Int] = ??? | ||
} | ||
} | ||
|
||
object Test { | ||
def test(xs: Array[Int]): Int = xs match { | ||
case Array2(x, y) => 1 // error | ||
case Array2(x, y, xs: _*) => 2 // error | ||
case Array2(xs: _*) => 3 // error | ||
} | ||
|
||
def test2(xs: Array[Int]): Int = xs match { | ||
case Array3(x, y) => 1 // error | ||
case Array3(x, y, xs: _*) => 2 // error | ||
case Array3(xs: _*) => 3 // error | ||
} | ||
} |
This file contains 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 Array2 { | ||
def unapplySeq[T](x: Array[T]): UnapplySeqWrapper[T] = new UnapplySeqWrapper(x) | ||
|
||
final class UnapplySeqWrapper[T](private val a: Array[T]) extends AnyVal { | ||
def isEmpty: Boolean = false | ||
def get: UnapplySeqWrapper[T] = this | ||
def lengthCompare(len: Int): Int = a.lengthCompare(len) | ||
def apply(i: Int): T = a(i) | ||
def drop(n: Int): scala.Seq[T] = ??? | ||
def toSeq: scala.Seq[T] = a.toSeq // clones the array | ||
} | ||
} | ||
|
||
class Test { | ||
def test1(xs: Array[Int]): Int = xs match { | ||
case Array2(x, y) => x + y | ||
} | ||
|
||
def test2(xs: Array[Int]): Seq[Int] = xs match { | ||
case Array2(x, y, xs:_*) => xs | ||
} | ||
|
||
def test3(xs: Array[Int]): Seq[Int] = xs match { | ||
case Array2(xs:_*) => xs | ||
} | ||
} |
This file contains 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,32 @@ | ||
object Array2 { | ||
def unapplySeq(x: Array[Int]): Data = new Data | ||
|
||
final class Data { | ||
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. Can we also a add test with: class Data1 {
def isEmpty: Boolean = false
def get: Data2 = new Data2
def lengthCompare(len: Int): Int = 0
}
}
class Data2 {
def apply(i: Int): Int = 3
def drop(n: Int): scala.Seq[Int] = Seq(2, 5)
def toSeq: scala.Seq[Int] = Seq(6, 7)
} |
||
def isEmpty: Boolean = false | ||
def get: Data = this | ||
def lengthCompare(len: Int): Int = 0 | ||
def apply(i: Int): Int = 3 | ||
def drop(n: Int): scala.Seq[Int] = Seq(2, 5) | ||
def toSeq: scala.Seq[Int] = Seq(6, 7) | ||
} | ||
} | ||
|
||
object Test { | ||
def test1(xs: Array[Int]): Int = xs match { | ||
case Array2(x, y) => x + y | ||
} | ||
|
||
def test2(xs: Array[Int]): Seq[Int] = xs match { | ||
case Array2(x, y, xs:_*) => xs | ||
} | ||
|
||
def test3(xs: Array[Int]): Seq[Int] = xs match { | ||
case Array2(xs:_*) => xs | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
test1(Array(3, 5)) | ||
test2(Array(3, 5)) | ||
test3(Array(3, 5)) | ||
} | ||
} |
This file contains 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,32 @@ | ||
object Array2 { | ||
def unapplySeq(x: Array[Int]): Data = new Data | ||
|
||
final class Data { | ||
def isEmpty: Boolean = false | ||
def get: Data = this | ||
def length: Int = 2 | ||
def apply(i: Int): Int = 3 | ||
def drop(n: Int): scala.Seq[Int] = Seq(2, 5) | ||
def toSeq: scala.Seq[Int] = Seq(6, 7) | ||
} | ||
} | ||
|
||
object Test { | ||
def test1(xs: Array[Int]): Int = xs match { | ||
case Array2(x, y) => x + y | ||
} | ||
|
||
def test2(xs: Array[Int]): Seq[Int] = xs match { | ||
case Array2(x, y, xs:_*) => xs | ||
} | ||
|
||
def test3(xs: Array[Int]): Seq[Int] = xs match { | ||
case Array2(xs:_*) => xs | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
test1(Array(3, 5)) | ||
test2(Array(3, 5)) | ||
test3(Array(3, 5)) | ||
} | ||
} |
This file contains 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,35 @@ | ||
object Array2 { | ||
def unapplySeq(x: Array[Int]): Data1 = new Data1 | ||
|
||
class Data1 { | ||
def isEmpty: Boolean = false | ||
def get: Data2 = new Data2 | ||
} | ||
|
||
class Data2 { | ||
def apply(i: Int): Int = 3 | ||
def drop(n: Int): scala.Seq[Int] = Seq(2, 5) | ||
def toSeq: scala.Seq[Int] = Seq(6, 7) | ||
def lengthCompare(len: Int): Int = 0 | ||
} | ||
} | ||
|
||
object Test { | ||
def test1(xs: Array[Int]): Int = xs match { | ||
case Array2(x, y) => x + y | ||
} | ||
|
||
def test2(xs: Array[Int]): Seq[Int] = xs match { | ||
case Array2(x, y, xs:_*) => xs | ||
} | ||
|
||
def test3(xs: Array[Int]): Seq[Int] = xs match { | ||
case Array2(xs:_*) => xs | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
test1(Array(3, 5)) | ||
test2(Array(3, 5)) | ||
test3(Array(3, 5)) | ||
} | ||
} |
This file contains 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,36 @@ | ||
object Array2 { | ||
def unapplySeq(x: Array[Int]): Data = new Data | ||
|
||
final class Data { | ||
def isEmpty: Boolean = false | ||
def get: Data = this | ||
def lengthCompare(len: Int): Int = 0 | ||
def lengthCompare: Int = 0 | ||
def apply(i: Int): Int = 3 | ||
def apply(i: String): Int = 3 | ||
def drop(n: Int): scala.Seq[Int] = Seq(2, 5) | ||
def drop: scala.Seq[Int] = Seq(2, 5) | ||
def toSeq: scala.Seq[Int] = Seq(6, 7) | ||
def toSeq(x: Int): scala.Seq[Int] = Seq(6, 7) | ||
} | ||
} | ||
|
||
object Test { | ||
def test1(xs: Array[Int]): Int = xs match { | ||
case Array2(x, y) => x + y | ||
} | ||
|
||
def test2(xs: Array[Int]): Seq[Int] = xs match { | ||
case Array2(x, y, xs:_*) => xs | ||
} | ||
|
||
def test3(xs: Array[Int]): Seq[Int] = xs match { | ||
case Array2(xs:_*) => xs | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
test1(Array(3, 5)) | ||
test2(Array(3, 5)) | ||
test3(Array(3, 5)) | ||
} | ||
} |
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.
Why not simply:
.select(nme.toSeq)
?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.
The code is defensive here to support possible overloading of
toSeq
.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.
We should add tests for this then. Would it work to do
.select(nme.toSeq).appliedToNone()
?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.
My hunch is that it will crash the compiler, as
appliedToNone
will not do overloading resolution.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.
But there is no need right? You cannot have two overloads of a method that takes no parameter
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.
Never mind, your solution seems better.
matchingMember
is already used in multiple places in this file (I suppose for reasons)