aboutsummaryrefslogtreecommitdiff
path: root/tests/pos/Patterns.scala
blob: fd0d7e97ace4b37ba87796eed3807ebe9af205ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import dotty.tools.dotc.ast.Trees._
import dotty.tools.dotc.core.Types._

object Patterns {
  val d: Object = null
  private def rebase(tp: NamedType): Type = {
    def rebaseFrom(prefix: Type): Type = ???
    tp.prefix match {
      case SkolemType(rt) => rebaseFrom(rt)
      case pre: ThisType => rebaseFrom(pre)
      case _ => tp
    }
  }
  d match {
    case WildcardType(bounds: TypeBounds) =>
      bounds.variance
    case a @ Assign(Ident(id), rhs) => id
    case a: Object => a
  }

  ('1', "1") match {
    case (digit, str) => true
    case _ => false
  }


  def foo2(x: AnyRef) = x match { case x: Function0[Any] => x() }
  object Breakdown {
    def unapplySeq(x: Int): Some[List[String]] = Some(List("", "there"))
  }

  object Test2 {
    42 match {
      case a@Breakdown(f@"") =>  // needed to trigger bug
      case b@Breakdown(d@"foo") =>  // needed to trigger bug
      case c@Breakdown(e@"", who) => println ("hello " + who)
    }
  }

  val names = List("a", "b", "c")
  object SeqExtractors {
    val y = names match {
      case List(x, z) => x
      case List(x) => x
      case List() => ""
      case x @ _ => "wildcard"
    }
    val yy: String = y
  }



  val xs = List('2' -> "ABC", '3' -> "DEF")

  xs filter {
    case (digit, str) => true
    case _ => false
  }

  (xs: Any) match {
    case x: Int @unchecked => true
    case xs: List[Int @ unchecked] => true
    case _ => false
  }

  def sum(xs: List[Int]): Int = xs match {
    case Nil => 0
    case x :: xs1 => x + sum(xs1)
  }

  def len[T](xs: List[T]): Int = xs match {
    case _ :: xs1 => 1 + len(xs1)
    case Nil => 0
  }

  final def sameLength[T](xs: List[T], ys: List[T]): Boolean = xs match {
    case _ :: xs1 => xs1.isEmpty
      ys match {
        case _ :: ys1 => sameLength(xs1, ys1)
        case _ => false
      }
    case _ => ys.isEmpty
  }

  class A{
    class B
  }
  val a1 = new A
  val a2 = new A
  d match {
    case t: a1.B =>
      t
    case t: a2.B =>
      t
  }

  class caseWithPatternVariableHelper1[A]
  class caseWithPatternVariableHelper2[A]

  def caseWithPatternVariable(x: Any) = x match {
    case a: caseWithPatternVariableHelper1[_] => ()
    case b: caseWithPatternVariableHelper2[_] => ()
  }

}

object NestedPattern {
  val xss: List[List[String]] = ???
  val List(List(x)) = xss
}

// Tricky case (exercised by Scala parser combinators) where we use
// both get/isEmpty and product-based pattern matching in different
// matches on the same types.
object ProductAndGet {

  trait Result[+T]
  case class Success[+T](in: String, x: T) extends Result[T] {
    def isEmpty = false
    def get: T = x
  }
  case class Failure[+T](in: String, msg: String) extends Result[T] {
    def isEmpty = false
    def get: String = msg
  }

  val r: Result[Int] = ???

  r match {
    case Success(in, x) => x
    case Failure(in, msg) => -1
  }

  r match {
    case Success(x) => x
    case Failure(msg) => -1
  }
}