aboutsummaryrefslogtreecommitdiff
path: root/tests/pending/run/virtpatmat_typetag.scala
blob: 3654decda0f47f96c12746411f8ef0b2da9ddf2d (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
import reflect.{ClassTag, classTag}

trait Extractors {
  type T
  implicit val tTag: ClassTag[T]
  object ExtractT {
    def unapply(x: T) = Some(x)
  }
  def apply(a: Any) = a match {
    case ExtractT(x)  => println(x +" is a "+ implicitly[ClassTag[T]])
    case _ => println(a+ " is not a "+ implicitly[ClassTag[T]] +"; it's a "+ a.getClass)
  }
}

object Test extends dotty.runtime.LegacyApp {
  def typeMatch[T: ClassTag](a: Any) = a match {
    case x : T => println(x +" is a "+ implicitly[ClassTag[T]])
    case _ => println(a+ " is not a "+ implicitly[ClassTag[T]] +"; it's a "+ a.getClass)
  }

  // the same match as typeMatch, but using an extractor
  def extractorMatch[S: ClassTag](a: Any) =
    (new Extractors { type T = S; val tTag = classTag[T] })(a)

  typeMatch[Int](1)
  typeMatch[Integer](1)
  typeMatch[String](1)
  typeMatch[Any](true)
  typeMatch[String]("woele")

  extractorMatch[Int](1)
  extractorMatch[Integer](1)
  extractorMatch[String](1)
  extractorMatch[Any](true)
  extractorMatch[String]("woele")
}