@@ -36,12 +36,12 @@ import scala.quoted._
36
36
inline def assert (expr : => Boolean ): Unit =
37
37
$ { assertImpl(' expr ) }
38
38
39
- def assertImpl (expr : Expr [Boolean ])(given QuoteContext ) = ' {
39
+ def assertImpl (expr : Expr [Boolean ])(using QuoteContext ) = ' {
40
40
if (! $expr)
41
41
throw new AssertionError (s " failed assertion: ${$ { showExpr(expr) }}" )
42
42
}
43
43
44
- def showExpr (expr : Expr [Boolean ])(given QuoteContext ): Expr [String ] =
44
+ def showExpr (expr : Expr [Boolean ])(using QuoteContext ): Expr [String ] =
45
45
' { " <some source code>" } // Better implementation later in this document
46
46
```
47
47
@@ -171,7 +171,7 @@ Indeed, the definition of `reflect` above uses `T` in the next stage, there is a
171
171
quote but no splice between the parameter binding of ` T ` and its
172
172
usage. But the code can be rewritten by adding a binding of a ` Type[T] ` tag:
173
173
``` scala
174
- def reflect [T , U ](f : Expr [T ] => Expr [U ])(given t : Type [T ]): Expr [T => U ] =
174
+ def reflect [T , U ](f : Expr [T ] => Expr [U ])(using t : Type [T ]): Expr [T => U ] =
175
175
' { (x : $t) => $ { f(' x ) } }
176
176
```
177
177
In this version of ` reflect ` , the type of ` x ` is now the result of
@@ -250,7 +250,7 @@ package quoted
250
250
251
251
object Expr {
252
252
...
253
- def apply [T : Liftable ](x : T )(given QuoteContext ): Expr [T ] = summon[Liftable [T ]].toExpr(x)
253
+ def apply [T : Liftable ](x : T )(using QuoteContext ): Expr [T ] = summon[Liftable [T ]].toExpr(x)
254
254
...
255
255
}
256
256
```
@@ -304,7 +304,7 @@ analogue of lifting.
304
304
305
305
Using lifting, we can now give the missing definition of ` showExpr ` in the introductory example:
306
306
``` scala
307
- def showExpr [T ](expr : Expr [T ])(given QuoteContext ): Expr [String ] = {
307
+ def showExpr [T ](expr : Expr [T ])(using QuoteContext ): Expr [String ] = {
308
308
val code : String = expr.show
309
309
Expr (code)
310
310
}
@@ -425,12 +425,12 @@ implementation of the `power` function that makes use of a statically known expo
425
425
``` scala
426
426
inline def power (x : Double , inline n : Int ) = $ { powerCode(' x , ' n ) }
427
427
428
- private def powerCode (x : Expr [Double ], n : Expr [Int ])(given QuoteContext ): Expr [Double ] =
428
+ private def powerCode (x : Expr [Double ], n : Expr [Int ])(using QuoteContext ): Expr [Double ] =
429
429
n.getValue match
430
430
case Some (m) => powerCode(x, m)
431
431
case None => ' { Math .pow($x, $y) }
432
432
433
- private def powerCode (x : Expr [Double ], n : Int )(given QuoteContext ): Expr [Double ] =
433
+ private def powerCode (x : Expr [Double ], n : Int )(using QuoteContext ): Expr [Double ] =
434
434
if (n == 0 ) ' { 1.0 }
435
435
else if (n == 1 ) x
436
436
else if (n % 2 == 0 ) ' { val y = $x * $x; $ { powerCode(' y , n / 2 ) } }
@@ -570,7 +570,7 @@ in a quote context. For this we simply provide `scala.quoted.matching.summonExpr
570
570
``` scala
571
571
inline def setFor [T ]: Set [T ] = $ { setForExpr[T ] }
572
572
573
- def setForExpr [T : Type ](given QuoteContext ): Expr [Set [T ]] = {
573
+ def setForExpr [T : Type ](using QuoteContext ): Expr [Set [T ]] = {
574
574
summonExpr[Ordering [T ]] match {
575
575
case Some (ord) => ' { new TreeSet [T ]()($ord) }
576
576
case _ => ' { new HashSet [T ] }
@@ -587,7 +587,7 @@ inline method that can calculate either a value of type `Int` or a value of type
587
587
``` scala
588
588
inline def defaultOf (inline str : String ) <: Any = $ { defaultOfImpl(' str ) }
589
589
590
- def defaultOfImpl (strExpr : Expr [String ])(given QuoteContext ): Expr [Any ] =
590
+ def defaultOfImpl (strExpr : Expr [String ])(using QuoteContext ): Expr [Any ] =
591
591
strExpr.value match
592
592
case " int" => ' {1 }
593
593
case " string" => ' {" a" }
@@ -627,7 +627,7 @@ In `scala.quoted.matching` contains object that can help extract values from `Ex
627
627
These could be used in the following way to optimize any call to ` sum ` that has statically known values.
628
628
``` scala
629
629
inline def sum (args : => Int * ): Int = $ { sumExpr(' args ) }
630
- private def sumExpr (argsExpr : Expr [Seq [Int ]])(given QuoteContext ): Expr [Int ] = argsExpr.underlyingArgument match {
630
+ private def sumExpr (argsExpr : Expr [Seq [Int ]])(using QuoteContext ): Expr [Int ] = argsExpr.underlyingArgument match {
631
631
case ConstSeq (args) => // args is of type Seq[Int]
632
632
Expr (args.sum) // precompute result of sum
633
633
case ExprSeq (argExprs) => // argExprs is of type Seq[Expr[Int]]
@@ -660,7 +660,7 @@ optimize {
660
660
``` scala
661
661
def sum (args : Int * ): Int = args.sum
662
662
inline def optimize (arg : Int ): Int = $ { optimizeExpr(' arg ) }
663
- private def optimizeExpr (body : Expr [Int ])(given QuoteContext ): Expr [Int ] = body match {
663
+ private def optimizeExpr (body : Expr [Int ])(using QuoteContext ): Expr [Int ] = body match {
664
664
// Match a call to sum without any arguments
665
665
case ' { sum() } => Expr (0 )
666
666
// Match a call to sum with an argument $n of type Int. n will be the Expr[Int] representing the argument.
@@ -669,7 +669,7 @@ private def optimizeExpr(body: Expr[Int])(given QuoteContext): Expr[Int] = body
669
669
case ' { sum($ {ExprSeq (args)}: _* ) } => sumExpr(args)
670
670
case body => body
671
671
}
672
- private def sumExpr (args1 : Seq [Expr [Int ]])(given QuoteContext ): Expr [Int ] = {
672
+ private def sumExpr (args1 : Seq [Expr [Int ]])(using QuoteContext ): Expr [Int ] = {
673
673
def flatSumArgs (arg : Expr [Int ]): Seq [Expr [Int ]] = arg match {
674
674
case ' { sum($ {ExprSeq (subArgs)}: _* ) } => subArgs.flatMap(flatSumArgs)
675
675
case arg => Seq (arg)
@@ -692,7 +692,7 @@ private def sumExpr(args1: Seq[Expr[Int]])(given QuoteContext): Expr[Int] = {
692
692
Sometimes it is necessary to get a more precise type for an expression. This can be achived using the following pattern match.
693
693
694
694
``` scala
695
- def f (exp : Expr [Any ])(given QuoteContext ) =
695
+ def f (exp : Expr [Any ])(using QuoteContext ) =
696
696
expr match
697
697
case ' { $x : $t } =>
698
698
// If the pattern match succeeds, then there is some type `T` such that
@@ -707,7 +707,7 @@ This might be used to then perform an implicit search as in:
707
707
``` scala
708
708
inline def (sc : StringContext ).showMe(args : => Any * ): String = $ { showMeExpr(' sc , ' args ) }
709
709
710
- private def showMeExpr (sc : Expr [StringContext ], argsExpr : Expr [Seq [Any ]])(given qctx : QuoteContext ): Expr [String ] = {
710
+ private def showMeExpr (sc : Expr [StringContext ], argsExpr : Expr [Seq [Any ]])(using qctx : QuoteContext ): Expr [String ] = {
711
711
argsExpr match {
712
712
case ExprSeq (argExprs) =>
713
713
val argShowedExprs = argExprs.map {
0 commit comments