|
1 |
| -class T1 |
| 1 | +// Product component types, and the sequence element type |
| 2 | +final class A; final class B; final class C |
| 3 | +final class E |
2 | 4 |
|
3 |
| -class P1 |
4 |
| -final class P2 |
5 |
| -class P3 |
| 5 | +// Conforms to both sequence matches and product sequence matches |
| 6 | +class Both extends Product1[A]: |
| 7 | + def length: Int = toSeq.length |
| 8 | + def apply(i: Int): E = toSeq.apply(i) |
| 9 | + def drop(n: Int): Seq[E] = toSeq.drop(n) |
| 10 | + def toSeq: Seq[E] = Seq(new E, new E) |
6 | 11 |
|
7 |
| -class E1 |
8 |
| -class E2 extends E1 |
9 |
| -class E3 extends E1 |
| 12 | + def canEqual(that: Any) = that.isInstanceOf[Both @unchecked] |
10 | 13 |
|
11 |
| -object VarExt: |
12 |
| - def unapplySeq(t1: T1): U1 = new U1 |
| 14 | + val _1: A = new A |
| 15 | + val _2: B = new B |
| 16 | + val _3: Seq[C] = Seq(new C) |
13 | 17 |
|
14 |
| -class U1 extends Product1[P1]: |
15 |
| - def canEqual(that: Any): Boolean = ??? |
| 18 | +// Like Both, but with a missing _2 |
| 19 | +class AlmostBoth extends Product1[A]: |
| 20 | + def length: Int = toSeq.length |
| 21 | + def apply(i: Int): E = toSeq.apply(i) |
| 22 | + def drop(n: Int): Seq[E] = toSeq.drop(n) |
| 23 | + def toSeq: Seq[E] = Seq(new E, new E) |
16 | 24 |
|
17 |
| - val _1: P1 = new P1 |
18 |
| - val _2: P2 = new P2 |
19 |
| - val _3: Seq[P3] = Seq(new P3) |
| 25 | + def canEqual(that: Any) = that.isInstanceOf[AlmostBoth @unchecked] |
20 | 26 |
|
21 |
| - def length: Int = ??? |
22 |
| - def apply(i: Int): E1 = ??? |
23 |
| - def drop(n: Int): Seq[E2] = ??? |
24 |
| - def toSeq: Seq[E3] = ??? |
| 27 | + val _1: A = new A |
| 28 | + val _3: Seq[C] = Seq(new C) |
25 | 29 |
|
26 |
| -class Test: |
27 |
| - def m1(t1: T1): Unit = t1 match |
28 |
| - case VarExt(c1, cs*) => // CCE: class P1 cannot be cast to class E1 |
29 |
| - val e1: E1 = c1 |
30 |
| - val e1s: Seq[E1] = cs |
| 30 | +// An extractor result holder, to return Both or BothAlmost |
| 31 | +class GetBoth { def isEmpty: Boolean = false; def get = new Both } |
| 32 | +class GetAlmostBoth { def isEmpty: Boolean = false; def get = new AlmostBoth } |
31 | 33 |
|
32 |
| -object Main: |
33 |
| - def main(args: Array[String]): Unit = |
34 |
| - new Test().m1(new T1) |
| 34 | +// The extractors |
| 35 | +object Both { def unapplySeq(x: Any): Both = new Both } |
| 36 | +object AlmostBoth { def unapplySeq(x: Any): Both = new Both } |
| 37 | +object GetBoth { def unapplySeq(x: Any): GetBoth = new GetBoth } |
| 38 | +object GetAlmostBoth { def unapplySeq(x: Any): GetAlmostBoth = new GetAlmostBoth } |
35 | 39 |
|
| 40 | +class Test: |
| 41 | + def t1a(x: Any): Seq[E] = x match { case Both(es*) => es } |
| 42 | + def t1b(x: Any): Seq[E] = x match { case AlmostBoth(es*) => es } |
| 43 | + def t1c(x: Any): Seq[E] = x match { case GetBoth(es*) => es } |
| 44 | + def t1d(x: Any): Seq[E] = x match { case GetAlmostBoth(es*) => es } |
| 45 | + |
| 46 | + def t2a(x: Any): (E, Seq[E]) = x match { case Both(e, es*) => (e, es) } |
| 47 | + def t2b(x: Any): (E, Seq[E]) = x match { case AlmostBoth(e, es*) => (e, es) } |
| 48 | + def t2c(x: Any): (E, Seq[E]) = x match { case GetBoth(e, es*) => (e, es) } |
| 49 | + def t2d(x: Any): (E, Seq[E]) = x match { case GetAlmostBoth(e, es*) => (e, es) } |
0 commit comments