Skip to content

Commit 916b754

Browse files
committed
Only define unapply
1 parent a13d99d commit 916b754

File tree

5 files changed

+20
-34
lines changed

5 files changed

+20
-34
lines changed

library/src/scala/tasty/TypeTest.scala

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ package scala.tasty
1414
@scala.annotation.implicitNotFound(msg = "No TypeTest available for [${S}, ${T}]")
1515
trait TypeTest[S, T <: S] extends Serializable {
1616

17-
def isInstance(x: S): TypeTest.Result[x.type & T]
18-
1917
/** A TypeTest[S, T] can serve as an extractor that matches only S of type T.
2018
*
2119
* The compiler tries to turn unchecked type tests in pattern matches into checked ones
@@ -24,18 +22,6 @@ trait TypeTest[S, T <: S] extends Serializable {
2422
* `SomeExtractor(...)` is turned into `tt(SomeExtractor(...))` if `T` in `SomeExtractor.unapply(x: T)`
2523
* is uncheckable, but we have an instance of `TypeTest[S, T]`.
2624
*/
27-
def unapply(x: S): Option[x.type & T] =
28-
if isInstance(x).asInstanceOf[Boolean] then Some(x.asInstanceOf[x.type & T])
29-
else None
30-
31-
}
32-
33-
object TypeTest {
34-
35-
opaque type Result[A] = Boolean
36-
37-
def success[A](x: A): Result[A] = true
38-
39-
def failure[A]: Result[A] = false
25+
def unapply(x: S): Option[x.type & T]
4026

4127
}

tests/neg-custom-args/fatal-warnings/type-test-paths-2.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ object RI extends R {
1515
type Succ = Int
1616
type Idx = Int
1717
protected def typeTestOfSucc: TypeTest[Nat, Succ] = new {
18-
def isInstance(x: Int): TypeTest.Result[x.type & Succ] =
19-
if x > 0 then TypeTest.success(x) else TypeTest.failure
18+
def unapply(x: Int): Option[x.type & Succ] =
19+
if x > 0 then Some(x) else None
2020
}
2121
def n: Nat = 4
2222
def one: Succ = 1

tests/neg-custom-args/fatal-warnings/type-test-paths.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ object T1 extends T {
3232
def x: X = false
3333
def y: Y = true
3434
protected def typeTestOfY: TypeTest[X, Y] = new {
35-
def isInstance(x: X): TypeTest.Result[x.type & Y] = x match
36-
case x: (true & x.type) => TypeTest.success(x)
37-
case _ => TypeTest.failure
35+
def unapply(x: X): Option[x.type & Y] = x match
36+
case x: (true & x.type) => Some(x)
37+
case _ => None
3838
}
3939

4040
}

tests/run/type-test-binding.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ sealed trait Foo {
1010
def f(y: Y) = println("ok")
1111

1212
given TypeTest[X, Y] = new TypeTest {
13-
def isInstance(x: X): TypeTest.Result[x.type & Y] =
14-
TypeTest.success(x.asInstanceOf[x.type & Y])
13+
def unapply(x: X): Option[x.type & Y] =
14+
Some(x.asInstanceOf[x.type & Y])
1515
}
1616

1717
object Z {

tests/run/type-test-nat.scala

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ object IntNums extends Peano {
5656
type Succ = Int
5757

5858
protected def typeTestOfZero: TypeTest[Nat, Zero] = new {
59-
def isInstance(x: Nat): TypeTest.Result[x.type & Zero] =
60-
if x == 0 then TypeTest.success(x)
61-
else TypeTest.failure
59+
def unapply(x: Nat): Option[x.type & Zero] =
60+
if x == 0 then Some(x)
61+
else None
6262
}
6363

6464
protected def typeTestOfSucc: TypeTest[Nat, Succ] = new {
65-
def isInstance(x: Nat): TypeTest.Result[x.type & Succ] =
66-
if x > 0 then TypeTest.success(x)
67-
else TypeTest.failure
65+
def unapply(x: Nat): Option[x.type & Succ] =
66+
if x > 0 then Some(x)
67+
else None
6868
}
6969

7070
def safeDiv(m: Nat, n: Succ): (Nat, Nat) = (m / n, m % n)
@@ -92,15 +92,15 @@ object ClassNums extends Peano {
9292
type Succ = SuccClass
9393

9494
protected def typeTestOfZero: TypeTest[Nat, Zero] = new {
95-
def isInstance(x: Nat): TypeTest.Result[x.type & Zero] = x match
96-
case x: (ZeroObject.type & x.type) => TypeTest.success(x)
97-
case _ => TypeTest.failure
95+
def unapply(x: Nat): Option[x.type & Zero] = x match
96+
case x: (ZeroObject.type & x.type) => Some(x)
97+
case _ => None
9898
}
9999

100100
protected def typeTestOfSucc: TypeTest[Nat, Succ] = new {
101-
def isInstance(x: Nat): TypeTest.Result[x.type & Succ] = x match
102-
case x: (SuccClass & x.type) => TypeTest.success(x)
103-
case _ => TypeTest.failure
101+
def unapply(x: Nat): Option[x.type & Succ] = x match
102+
case x: (SuccClass & x.type) => Some(x)
103+
case _ => None
104104
}
105105

106106
def safeDiv(m: Nat, n: Succ): (Nat, Nat) = {

0 commit comments

Comments
 (0)