Skip to content

Commit 905c541

Browse files
committed
Merge pull request #541 from dotty-staging/fix/#537-extractors
Drop the requirement that extractors with `get` must implement Product
2 parents a52ca60 + 8f5f07f commit 905c541

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,12 @@ object Applications {
5151
sels.takeWhile(_.exists).toList
5252
}
5353

54-
def getUnapplySelectors(tp: Type, args:List[untpd.Tree], pos: Position = NoPosition)(implicit ctx: Context): List[Type] =
55-
if (defn.isProductSubType(tp) && args.length > 1) productSelectorTypes(tp, pos)
56-
else tp :: Nil
54+
def getUnapplySelectors(tp: Type, args: List[untpd.Tree], pos: Position = NoPosition)(implicit ctx: Context): List[Type] =
55+
if (args.length > 1 && !(tp.derivesFrom(defn.SeqClass))) {
56+
val sels = productSelectorTypes(tp, pos)
57+
if (sels.length == args.length) sels
58+
else tp :: Nil
59+
} else tp :: Nil
5760

5861
def unapplyArgs(unapplyResult: Type, unapplyFn:Tree, args:List[untpd.Tree], pos: Position = NoPosition)(implicit ctx: Context): List[Type] = {
5962

tests/pos/extractors.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
object test {
2+
3+
class Tree
4+
class Apply(val fun: Tree, val args: List[Tree]) extends Tree
5+
6+
trait DeconstructorCommon[T >: Null <: AnyRef] {
7+
var field: T = null
8+
def get: this.type = this
9+
def isEmpty: Boolean = field eq null
10+
def isDefined = !isEmpty
11+
def unapply(s: T): this.type ={
12+
field = s
13+
this
14+
}
15+
}
16+
17+
trait ApplyDeconstructor extends DeconstructorCommon[Apply] {
18+
def _1: Tree
19+
def _2: List[Tree]
20+
}
21+
22+
object Apply extends ApplyDeconstructor {
23+
def _1: Tree = field.fun
24+
def _2: List[Tree] = field.args
25+
}
26+
27+
def assocsFromApply(tree: Tree) = {
28+
tree match {
29+
case Apply(fun, args) => ???
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)