File tree Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ import scala .quoted .*
2
+ import language .experimental .erasedDefinitions
3
+
4
+ inline def testExpr (inline body : Any ) = $ { testExprImpl(' body ) }
5
+ def testExprImpl (body : Expr [Any ])(using Quotes ): Expr [String ] =
6
+ body match
7
+ case ' { def g (y : String ) = " placeholder" + y; $a(g): String } =>
8
+ ' { $a((z : String ) => s " [1st case] ${z}" ) }
9
+ case ' { def g (y : String )(z : String ) = " placeholder" + y; $a(g): String } =>
10
+ ' { $a((z1 : String ) => (z2 : String ) => s " [2nd case] ${z1}, ${z2}" ) }
11
+ // Refined Types
12
+ case ' {
13
+ type t
14
+ def refined (a : `t`): String = $x(a): String
15
+ $y(refined): String
16
+ } =>
17
+ ' { $y($x) }
18
+ // Dependent Types
19
+ case ' {
20
+ def p (dsl : DSL ): dsl.N = dsl.zero
21
+ $y(p): String
22
+ } =>
23
+ ' { $y((dsl1 : DSL ) => dsl1.next(dsl1.zero)) }
24
+ case ' {
25
+ def p (dsl : DSL )(a : dsl.N ): dsl.N = a
26
+ $y(p): String
27
+ } =>
28
+ ' { $y((dsl : DSL ) => (b2 : dsl.N ) => dsl.next(b2)) }
29
+ case ' {
30
+ def p (dsl1 : DSL )(dsl2 : DSL ): dsl2.N = dsl2.zero
31
+ $y(p): String
32
+ } =>
33
+ ' { $y((dsl1 : DSL ) => (dsl2 : DSL ) => dsl2.next(dsl2.zero)) }
34
+ case _ => Expr (" not matched" )
Original file line number Diff line number Diff line change
1
+ import reflect .Selectable .reflectiveSelectable
2
+
3
+ class Hoe { def f (x : Int ): String = s " Hoe got ${x}" }
4
+
5
+ @ main def Test : Unit =
6
+ println(" case single: " + testExpr { def f (x : String ) = " placeholder" + x; f(" arg1" ) + " outside" })
7
+ println(" case no-param-method (will be eta-expanded): " + testExpr { def f (x : String ) = " placeholder" + x; (() => f)()(" placeholder 2" ) })
8
+ println(" case curried: " + testExpr { def f (x : String )(y : String ) = " placeholder" + x; f(" arg1" )(" arg2" ) + " outside" })
9
+ def outer () = " outer-method"
10
+ println(" case methods from outer scope: " + testExpr { def f (x : String ) = " placeholder" + x; f(" arg1" ) + outer() })
11
+ println(" case refinement: " + testExpr { def refined (a : { def f (x : Int ): String }): String = a.f(1 ); refined(Hoe ()) })
12
+ println(" case dependent: " + testExpr {
13
+ def p (a : DSL ): a.N = a.zero
14
+ IntDSL .toString(p(IntDSL ))
15
+ })
16
+ println(" case dependent2: " + testExpr {
17
+ def p (dsl1 : DSL )(c : dsl1.N ): dsl1.N = c
18
+ IntDSL .toString(p(IntDSL )(IntDSL .zero))
19
+ })
20
+ println(" case dependent3: " + testExpr {
21
+ def p (dsl1 : DSL )(dsl2 : DSL ): dsl2.N = dsl2.zero
22
+ IntDSL .toString(p(IntDSL )(IntDSL ))
23
+ })
You can’t perform that action at this time.
0 commit comments