Skip to content

Commit 255c5bb

Browse files
liufengyunallanrenucci
authored andcommitted
update docs for name-based unapplySeq
1 parent 9304ed6 commit 255c5bb

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ object Applications {
125125
def validUnapplySeqType(getTp: Type): Boolean = unapplySeqTypeElemTp(getTp).exists
126126

127127
if (unapplyName == nme.unapplySeq) {
128-
if (unapplyResult derivesFrom defn.SeqClass) seqSelector :: Nil
129-
else if (isGetMatch(unapplyResult, pos) && validUnapplySeqType(getTp)) {
128+
if (isGetMatch(unapplyResult, pos) && validUnapplySeqType(getTp)) {
130129
val elemTp = unapplySeqTypeElemTp(getTp)
131130
args.map(Function.const(elemTp))
132131
}

docs/docs/reference/changed/pattern-matching.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,30 @@ object FirstChars {
6363
```
6464

6565

66-
## Seq Pattern
66+
## Name-based Seq Pattern
6767

6868
- Extractor defines `def unapplySeq(x: T): U`
6969
- `U` has (parameterless `def` or `val`) members `isEmpty: Boolean` and `get: S`
70-
- `S <: Seq[V]`
71-
- Pattern-matching on `N` pattern with types `V, V, ..., V`, where `N` is the runtime size of the `Seq`.
70+
- `S` conforms to `X` or `Y`, `T2` and `T3` conform to `T1`
71+
72+
```Scala
73+
type X = {
74+
def lengthCompare(len: Int): Int
75+
def apply(i: Int): T1 = a(i)
76+
def drop(n: Int): scala.Seq[T2]
77+
def toSeq: scala.Seq[T3]
78+
}
79+
80+
type Y = {
81+
def length: Int
82+
def apply(i: Int): T1 = a(i)
83+
def drop(n: Int): scala.Seq[T2]
84+
def toSeq: scala.Seq[T3]
85+
}
86+
```
87+
88+
- Pattern-matching on _exactly_ `N` simple patterns with types `T1, T1, ..., T1`, where `N` is the runtime size of the sequence, or
89+
- Pattern-matching on `>= N` simple patterns and _a vararg pattern_ (e.g., `xs: _*`) with types `T1, T1, ..., T1, Seq[T1]`, where `N` is the minimum size of the sequence.
7290

7391
<!-- To be kept in sync with tests/new/patmat-spec.scala -->
7492

@@ -87,7 +105,7 @@ object CharList {
87105
```
88106

89107

90-
## Name Based Pattern
108+
## Name-based Pattern
91109

92110
- Extractor defines `def unapply(x: T): U`
93111
- `U` has (parameterless `def` or `val`) members `isEmpty: Boolean` and `get: S`

0 commit comments

Comments
 (0)