|
| 1 | +--- |
| 2 | +layout: doc-page |
| 3 | +title: "TypeTest" |
| 4 | +--- |
| 5 | + |
| 6 | +TypeTest |
| 7 | +-------- |
| 8 | + |
| 9 | +When pattern matching there are two situations where were a runtime type test must be performed. |
| 10 | +The first is kind is an explicit type test using the ascription pattern notation. |
| 11 | +```scala |
| 12 | +(x: X) match |
| 13 | + case y: Y => |
| 14 | +``` |
| 15 | +The second is when an extractor takes an argument that is not a subtype of the scrutinee type. |
| 16 | +```scala |
| 17 | +(x: X) match |
| 18 | + case y @ Y(n) => |
| 19 | + |
| 20 | +object Y: |
| 21 | + def unapply(x: Y): Some[Int] = ... |
| 22 | +``` |
| 23 | + |
| 24 | +In both cases, a class test will be performed at runtime. |
| 25 | +But when the type test is on an abstract type (type parameter or type member), the test cannot be performed because the type is erased at runtime. |
| 26 | + |
| 27 | +A `TypeTest` can be provided to make this test possible. |
| 28 | + |
| 29 | +```scala |
| 30 | +package scala.reflect |
| 31 | + |
| 32 | +trait TypeTest[-S, T]: |
| 33 | + def unapply(s: S): Option[s.type & T] |
| 34 | +``` |
| 35 | + |
| 36 | +It provides an extractor that returns its argument typed as a `T` if the argument is a `T`. |
| 37 | +It can be used to encode a type test. |
| 38 | +```scala |
| 39 | +def f[X, Y](x: X)(using tt: TypeTest[X, Y]): Option[Y] = |
| 40 | + x match |
| 41 | + case tt(x @ Y(1)) => Some(x) |
| 42 | + case tt(x) => Some(x) |
| 43 | + case _ => None |
| 44 | +``` |
| 45 | + |
| 46 | +To avoid the syntactic overhead the compiler will look for a type test automatically if it detects that the type test is on abstract types. |
| 47 | +This means that `x: Y` is transformed to `tt(x)` and `x @ Y(_)` to `tt(x @ Y(_))` if there is a contextual `TypeTest[X, Y]` in scope. |
| 48 | +The previous code is equivalent to |
| 49 | + |
| 50 | +```scala |
| 51 | +def f[X, Y](x: X)(using TypeTest[X, Y]): Option[Y] = |
| 52 | + x match |
| 53 | + case x @ Y(1) => Some(x) |
| 54 | + case x: Y => Some(x) |
| 55 | + case _ => None |
| 56 | +``` |
| 57 | + |
| 58 | +We could create a type test at call site where the type test can be performed with runtime class tests directly as follows |
| 59 | + |
| 60 | +```scala |
| 61 | +val tt: TypeTest[Any, String] = |
| 62 | + new TypeTest[Any, String] |
| 63 | + def unapply(s: Any): Option[s.type & String] = |
| 64 | + s match |
| 65 | + case s: String => Some(s) |
| 66 | + case _ => None |
| 67 | + |
| 68 | +f[AnyRef, String]("acb")(using tt) |
| 69 | +``` |
| 70 | + |
| 71 | +The compiler will synthesize a new instance of a type test if none is found in scope as: |
| 72 | +```scala |
| 73 | +new TypeTest[A, B]: |
| 74 | + def unapply(s: A): Option[s.type & B] = |
| 75 | + s match |
| 76 | + case s: B => Some(s) |
| 77 | + case _ => None |
| 78 | +``` |
| 79 | +If the type tests cannot be done there will be an unchecked warning that will be raised on the `case s: B =>` test. |
| 80 | + |
| 81 | +The most common `TypeTest` instances are the ones that take any parameters (i.e. `TypeTest[Any, T]`). |
| 82 | +To make it possible to use such instances directly in context bounds we provide the alias |
| 83 | +```scala |
| 84 | +package scala.reflect |
| 85 | + |
| 86 | +type Typeable[T] = TypeTest[Any, T] |
| 87 | +``` |
| 88 | + |
| 89 | +This alias can be used as |
| 90 | + |
| 91 | +```scala |
| 92 | +def f[T: Typeable]: Boolean = |
| 93 | + "abc" match |
| 94 | + case x: T => true |
| 95 | + case _ => false |
| 96 | + |
| 97 | +f[String] // true |
| 98 | +f[Int] // fasle |
| 99 | +``` |
| 100 | + |
| 101 | +### TypeTest and ClassTag |
| 102 | +`TypeTest` is a replacement for functionality provided previously by `ClassTag.unapply`. |
| 103 | +Using `ClassTag` instances was unsound since classtags can check only the class component of a type. |
| 104 | +`TypeTest` fixes that unsoundness. |
| 105 | +`ClassTag` type tests are still supported but a warning will be emitted after 3.0. |
| 106 | + |
| 107 | + |
| 108 | +Examples |
| 109 | +-------- |
| 110 | + |
| 111 | +Given the following abstract definition of `Peano` numbers that provides `TypeTest[Nat, Zero]` and `TypeTest[Nat, Succ]` |
| 112 | + |
| 113 | +```scala |
| 114 | +trait Peano: |
| 115 | + type Nat |
| 116 | + type Zero <: Nat |
| 117 | + type Succ <: Nat |
| 118 | + def safeDiv(m: Nat, n: Succ): (Nat, Nat) |
| 119 | + val Zero: Zero |
| 120 | + val Succ: SuccExtractor |
| 121 | + trait SuccExtractor { |
| 122 | + def apply(nat: Nat): Succ |
| 123 | + def unapply(nat: Succ): Option[Nat] |
| 124 | + } |
| 125 | + given TypeTest[Nat, Zero] = typeTestOfZero |
| 126 | + protected def typeTestOfZero: TypeTest[Nat, Zero] |
| 127 | + given TypeTest[Nat, Succ] = typeTestOfSucc |
| 128 | + protected def typeTestOfSucc: TypeTest[Nat, Succ] |
| 129 | +``` |
| 130 | + |
| 131 | +it will be possible to write the following program |
| 132 | + |
| 133 | +```scala |
| 134 | +val peano: Peano = ... |
| 135 | +import peano._ |
| 136 | +def divOpt(m: Nat, n: Nat): Option[(Nat, Nat)] = |
| 137 | + n match |
| 138 | + case Zero => None |
| 139 | + case s @ Succ(_) => Some(safeDiv(m, s)) |
| 140 | + |
| 141 | +val two = Succ(Succ(Zero)) |
| 142 | +val five = Succ(Succ(Succ(two))) |
| 143 | +println(divOpt(five, two)) |
| 144 | +``` |
| 145 | + |
| 146 | +Note that without the `TypeTest[Nat, Succ]` the pattern `Succ.unapply(nat: Succ)` would be unchecked. |
0 commit comments