Skip to content

Commit f18ad26

Browse files
committed
Add TypeTest example
1 parent c93e2c1 commit f18ad26

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

library/src/scala/reflect/TypeTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ trait TypeTest[-S, T] extends Serializable:
2020
* `SomeExtractor(...)` is turned into `tt(SomeExtractor(...))` if `T` in `SomeExtractor.unapply(x: T)`
2121
* is uncheckable, but we have an instance of `TypeTest[S, T]`.
2222
*/
23-
def unapply(x: S): Option[x.type & T]
23+
def unapply(x: S): Option[x.type & T] @scala.annotation.covers[T]
2424

2525
object TypeTest:
2626

tests/patmat/i10961.scala

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ object test2lib {
3434

3535
type ParamClause = ValDefs | TypeDefs
3636

37-
object ValDefs with
37+
object ValDefs:
3838
def unapply(pc: ParamClause): Option[ValDefs] @covers[ValDefs] = ??? // matches empty list and all lists of ValDefs
3939

4040
def apply(vals: List[ValDef]): ValDefs = vals
4141

42-
object TypeDefs with
42+
object TypeDefs:
4343
def unapply(pc: ParamClause): Option[TypeDefs] @covers[TypeDefs] = ??? // matches non-empty lists of TypeDefs
4444

4545
def apply(tdefs: List[TypeDef]): TypeDefs =
@@ -64,15 +64,15 @@ object test3lib {
6464

6565
type Num = Nat | Neg
6666

67-
object Nat with
67+
object Nat:
6868
def unapply(x: Num): Option[Nat] @covers[Nat] =
6969
if x >= 0 then Some(x) else None
7070

7171
def apply(x: Int): Nat =
7272
assert(x >= 0)
7373
x
7474

75-
object Neg with
75+
object Neg:
7676
def unapply(x: Num): Option[Neg] @covers[Neg] =
7777
if x < 0 then Some(x) else None
7878

@@ -88,4 +88,13 @@ object test3 {
8888
x match
8989
case Nat(x) =>
9090
case Neg(x) =>
91+
}
92+
93+
object test4 {
94+
import scala.reflect.TypeTest
95+
96+
def test[A](a: A | String)(using TypeTest[A | String, A]) =
97+
a match
98+
case a: A =>
99+
case s: String =>
91100
}

0 commit comments

Comments
 (0)