File tree 1 file changed +41
-0
lines changed
1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments