-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explicitly call
toSeq
on wildcard-star patterns
With name-based pattern matching, unapplySeq can return any type that has an `isEmpty` and `get` methods. The object returned by `get` needs to have `apply`, `length` or `lengthCompare` and `drop` methods. This PR changes the type of `x @ _*` bound to `scala.Seq`. To support that change, the object returned by `unapplySeq.get` is converted by calling `.toSeq` or `drop(n)`. This means there are two changes in the interface for name-based pattern matching: - the object needs to define a `toSeq` method - the `drop` method needs to return a `scala.Seq` The `unapplySeq` method defined in `collection.SeqFactory` now returns a value class wrapper that delegates to the collection. `toSeq` no longer exposes mutable collections. `Array.unapplySeq` uses a similar value class wrapper.
- Loading branch information
Showing
15 changed files
with
582 additions
and
67 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
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,15 @@ | ||
patmat-seq-neg.scala:15: error: error during expansion of this match (this is a scalac bug). | ||
The underlying error was: type mismatch; | ||
found : scala.collection.mutable.ArrayBuffer[Int] | ||
required: Seq[Int] | ||
def t3: Any = 2 match { | ||
^ | ||
patmat-seq-neg.scala:18: error: error during expansion of this match (this is a scalac bug). | ||
The underlying error was: value toSeq is not a member of Array[Int] | ||
def t4: Any = 2 match { | ||
^ | ||
patmat-seq-neg.scala:24: error: error during expansion of this match (this is a scalac bug). | ||
The underlying error was: value drop is not a member of Array[Int] | ||
def t6: Any = 2 match { | ||
^ | ||
three errors found |
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,27 @@ | ||
object A { | ||
def unapplySeq(a: Int) = Some(collection.mutable.ArrayBuffer(1,2,3)) | ||
} | ||
object B { | ||
def unapplySeq(a: Int) = Some(Array(1,2,3)) | ||
} | ||
|
||
class T { | ||
def t1: Any = 2 match { | ||
case A(xs@_*) => xs // ok | ||
} | ||
def t2: Any = 2 match { | ||
case A(x, y) => (x, y) // ok | ||
} | ||
def t3: Any = 2 match { | ||
case A(x, xs@_*) => (x, xs) // type error with call to drop. found: ArrayBuffer, required: Seq. | ||
} | ||
def t4: Any = 2 match { | ||
case B(xs@_*) => xs // error: toSeq is not a member of Array. no ArrayOps because adaptToMember is disabled after typer. | ||
} | ||
def t5: Any = 2 match { | ||
case B(x, y) => (x, y) // ok | ||
} | ||
def t6: Any = 2 match { | ||
case B(x, xs@_*) => (x, xs) // error: drop is not a member of Array | ||
} | ||
} |
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
Oops, something went wrong.