summaryrefslogtreecommitdiff
path: root/test/files/run/patmat_unapp_abstype.scala
blob: e5adec5c1605a6ce144a1e33bf5695cc68a2273d (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
// abstract types and extractors, oh my!
trait TypesAPI {
  trait Type

  // an alternative fix (implemented in the virtual pattern matcher, is to replace the isInstanceOf by a manifest-based run-time test)
  // that's what typeRefMani is for
  type TypeRef <: Type //; implicit def typeRefMani: Manifest[TypeRef]
  val TypeRef: TypeRefExtractor; trait TypeRefExtractor {
    def apply(x: Int): TypeRef
    def unapply(x: TypeRef): Option[(Int)]
  }

  // just for illustration, should follow the same pattern as TypeRef
  case class MethodType(n: Int) extends Type
}

// user should not be exposed to the implementation
trait TypesUser extends TypesAPI {
  def shouldNotCrash(tp: Type): Unit = {
    tp match {
      case TypeRef(x) => println("TypeRef") 
      case MethodType(x) => println("MethodType")
      case _ => println("none of the above")
    }
  }
}

trait TypesImpl extends TypesAPI {
  object TypeRef extends TypeRefExtractor  // this will have a bridged unapply(x: Type) = unapply(x.asInstanceOf[TypeRef])
  case class TypeRef(n: Int) extends Type // this has a bridge from TypesAPI#Type to TypesImpl#TypeRef 
  // --> the cast in the bridge will fail because the pattern matcher can't type test against the abstract types in TypesUser
  //lazy val typeRefMani = manifest[TypeRef]
}

object Test extends TypesImpl with TypesUser with App {
  shouldNotCrash(TypeRef(10)) // should and does print "TypeRef"
  // once  #1697/#2337 are fixed, this should generate the correct output
  shouldNotCrash(MethodType(10)) // should print "MethodType" but prints "none of the above" -- good one, pattern matcher!
}