You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`Expr.betaReduce` returns an expression that is functionally equivalent to e, however if e is of the form `((y1, ..., yn) => e2)(e1, ..., en)` then it optimizes the top most call by returning the result of beta-reducing the application. Otherwise returns expr.
184
177
185
178
## Lifting Types
186
179
@@ -192,7 +185,7 @@ quote but no splice between the parameter binding of `T` and its
192
185
usage. But the code can be rewritten by adding an explicit binding of a `Type[T]`:
The `summon` query succeeds because there is a given instance of
226
-
type `Type[T]` available (namely the given parameter corresponding
227
-
to the context bound `: Type`), and the reference to that value is
218
+
The `summon` query succeeds because there is a using parameter of
219
+
type `Type[T]`, and the reference to that value is
228
220
phase-correct. If that was not the case, the phase inconsistency for
229
221
`T` would be reported as an error.
230
222
@@ -526,8 +518,8 @@ the code it runs produces one.
526
518
527
519
## Example Expansion
528
520
529
-
Assume we have two methods, one `map` that takes an `Expr[Array[T]]` and a
530
-
function `f` and one`sum` that performs a sum by delegating to `map`.
521
+
Assume we have two methods, `map` that takes an `Expr[Array[T]]` and a
522
+
function `f`, and `sum` that performs a sum by delegating to `map`.
531
523
532
524
```scala
533
525
objectMacros:
@@ -552,38 +544,66 @@ object Macros:
552
544
endMacros
553
545
```
554
546
555
-
A call to `sum_m(Array(1,2,3))` will first inline `sum_m`:
547
+
A call to `sum_m(Array(1, 2, 3))` will first inline `sum_m`:
556
548
557
549
```scala
558
-
valarr:Array[Int] =Array.apply(1, [2,3:Int]:Int*)
559
-
${_root_.Macros.sum('arr)}
550
+
valarr:Array[Int] =Array.apply(1, 2, 3)
551
+
${_root_.Macros.sum('arr)}
560
552
```
561
553
562
-
then it will splice`sum`:
554
+
then it will call`sum`:
563
555
564
556
```scala
565
-
valarr:Array[Int] =Array.apply(1, [2,3:Int]:Int*)
557
+
valarr:Array[Int] =Array.apply(1, 2, 3)
558
+
${ '{
559
+
varsum=0
560
+
${ map('arr, x =>'{sum += $x}) }
561
+
sum
562
+
} }
563
+
```
564
+
565
+
and cancel the `${'{...}}`:
566
+
567
+
```scala
568
+
valarr:Array[Int] =Array.apply(1, 2, 3)
566
569
567
570
varsum=0
568
571
${ map('arr, x =>'{sum += $x}) }
569
572
sum
570
573
```
571
574
572
-
then it will inline `map`:
575
+
then it will extract `x => '{sum += $x}` into `f`, to have a value:
573
576
574
577
```scala
575
-
valarr:Array[Int] =Array.apply(1, [2,3:Int]:Int*)
578
+
valarr:Array[Int] =Array.apply(1, 2, 3)
576
579
577
580
varsum=0
578
581
valf= x =>'{sum += $x}
579
-
${ _root_.Macros.map('arr, 'f)(Type.of[Int])}
582
+
${ _root_.Macros.map('arr, 'f)(Type.of[Int])}
580
583
sum
581
584
```
582
585
583
-
then it will expand and splice inside quotes`map`:
586
+
and then call`map`:
584
587
585
588
```scala
586
-
valarr:Array[Int] =Array.apply(1, [2,3:Int]:Int*)
589
+
valarr:Array[Int] =Array.apply(1, 2, 3)
590
+
591
+
varsum=0
592
+
valf= x =>'{sum += $x}
593
+
${ '{
594
+
vari:Int=0
595
+
while i < arr.length do
596
+
valelement:Int= (arr)(i)
597
+
sum += element
598
+
i +=1
599
+
sum
600
+
} }
601
+
```
602
+
603
+
and cancel the `${'{...}}` again:
604
+
605
+
```scala
606
+
valarr:Array[Int] =Array.apply(1, 2, 3)
587
607
588
608
varsum=0
589
609
valf= x =>'{sum += $x}
@@ -598,7 +618,7 @@ sum
598
618
Finally cleanups and dead code elimination:
599
619
600
620
```scala
601
-
valarr:Array[Int] =Array.apply(1, [2,3:Int]:Int*)
621
+
valarr:Array[Int] =Array.apply(1, 2, 3)
602
622
varsum=0
603
623
vari:Int=0
604
624
while i < arr.length do
@@ -662,7 +682,7 @@ It is possible to deconstruct or extract values out of `Expr` using pattern matc
662
682
663
683
`scala.quoted` contains objects that can help extracting values from `Expr`.
664
684
665
-
-`scala.quoted.Expr`/`scala.quoted.Exprs`: matches an expression of a value (or list of values) and returns the value (or list of values).
685
+
-`scala.quoted.Expr`/`scala.quoted.Exprs`: matches an expression of a value (resp. list of values) and returns the value (resp. list of values).
666
686
-`scala.quoted.Const`/`scala.quoted.Consts`: Same as `Expr`/`Exprs` but only works on primitive values.
667
687
-`scala.quoted.Varargs`: matches an explicit sequence of expressions and returns them. These sequences are useful to get individual `Expr[T]` out of a varargs expression of type `Expr[Seq[T]]`.
0 commit comments