Skip to content

Commit a07dd83

Browse files
nicolasstuckiKordyjan
authored andcommitted
Add regression test
This add a prototype implementation for providing precise types without the overhead of quote pattern matching. [Cherry-picked 4743d9c]
1 parent 97e2aa1 commit a07dd83

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import scala.quoted.*
2+
3+
def withType[T <: AnyKind, U](tpe: Type[T])(body: [X <: T] => Type[X] ?=> U)(using Quotes): U =
4+
type X <: T
5+
val tpeX: Type[X] = tpe.asInstanceOf[Type[X]]
6+
body[X](using tpeX)
7+
8+
def test1(t1: Type[?], t2: Type[? <: Any])(using Quotes) =
9+
withType(t1) { [T <: AnyKind] => _ ?=> // TODO remove _ ?=> // Implementation restriction: polymorphic function literals must have a value parameter
10+
Type.of[T]
11+
Type.show[T]
12+
}
13+
withType(t2) { [T] => _ ?=> // TODO remove _ ?=>
14+
'{ val a: T = ??? }
15+
Type.of[T]
16+
Type.show[T]
17+
}
18+
withType(t2):
19+
[T] => _ ?=> '{ val a: T = ??? } // TODO remove _ ?=>
20+
21+
def exprWithPreciseType[T, U](expr: Expr[T])(body: [X <: T] => Type[X] ?=> Expr[X] => U)(using Quotes): U =
22+
import quotes.reflect.*
23+
type X <: T
24+
val exprX = expr.asInstanceOf[Expr[X]]
25+
val tpeX = expr.asTerm.tpe.asType.asInstanceOf[Type[X]]
26+
body[X](using tpeX)(exprX)
27+
28+
def test2(x: Expr[Any])(using Quotes) =
29+
// exprWithPreciseType(x) { [T] => x => // Inference limitation: x is assumed to be the Type[T] instead of the Expr[T]
30+
exprWithPreciseType(x) { [T] => _ ?=> x => // TODO remove _ ?=>
31+
Type.of[T]
32+
'{ val a: T = $x }
33+
}
34+
exprWithPreciseType('{1}) { [T <: Int] => _ ?=> x => // TODO remove _ ?=>
35+
Type.of[T]
36+
'{ val a: Int = $x }
37+
'{ val a: T = $x }
38+
'{ val a: T = i($x) }
39+
}
40+
41+
def i[T <: Int](x: T): T = x

0 commit comments

Comments
 (0)