-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5078 from dotty-staging/fix-4984
Fix #4984: support name-based unapplySeq
- Loading branch information
Showing
10 changed files
with
274 additions
and
16 deletions.
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 { | ||
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)) | ||
} | ||
} |