@@ -36,12 +36,12 @@ import scala.quoted._
3636inline def assert (expr : => Boolean ): Unit =
3737 $ { assertImpl(' expr ) }
3838
39- def assertImpl (expr : Expr [Boolean ])(given QuoteContext ) = ' {
39+ def assertImpl (expr : Expr [Boolean ])(using QuoteContext ) = ' {
4040 if (! $expr)
4141 throw new AssertionError (s " failed assertion: ${$ { showExpr(expr) }}" )
4242}
4343
44- def showExpr (expr : Expr [Boolean ])(given QuoteContext ): Expr [String ] =
44+ def showExpr (expr : Expr [Boolean ])(using QuoteContext ): Expr [String ] =
4545 ' { " <some source code>" } // Better implementation later in this document
4646```
4747
@@ -171,7 +171,7 @@ Indeed, the definition of `reflect` above uses `T` in the next stage, there is a
171171quote but no splice between the parameter binding of ` T ` and its
172172usage. But the code can be rewritten by adding a binding of a ` Type[T] ` tag:
173173``` 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 ] =
175175 ' { (x : $t) => $ { f(' x ) } }
176176```
177177In this version of ` reflect ` , the type of ` x ` is now the result of
@@ -250,7 +250,7 @@ package quoted
250250
251251object Expr {
252252 ...
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)
254254 ...
255255}
256256```
@@ -304,7 +304,7 @@ analogue of lifting.
304304
305305Using lifting, we can now give the missing definition of ` showExpr ` in the introductory example:
306306``` scala
307- def showExpr [T ](expr : Expr [T ])(given QuoteContext ): Expr [String ] = {
307+ def showExpr [T ](expr : Expr [T ])(using QuoteContext ): Expr [String ] = {
308308 val code : String = expr.show
309309 Expr (code)
310310}
@@ -425,12 +425,12 @@ implementation of the `power` function that makes use of a statically known expo
425425``` scala
426426inline def power (x : Double , inline n : Int ) = $ { powerCode(' x , ' n ) }
427427
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 ] =
429429 n.getValue match
430430 case Some (m) => powerCode(x, m)
431431 case None => ' { Math .pow($x, $y) }
432432
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 ] =
434434 if (n == 0 ) ' { 1.0 }
435435 else if (n == 1 ) x
436436 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
570570``` scala
571571inline def setFor [T ]: Set [T ] = $ { setForExpr[T ] }
572572
573- def setForExpr [T : Type ](given QuoteContext ): Expr [Set [T ]] = {
573+ def setForExpr [T : Type ](using QuoteContext ): Expr [Set [T ]] = {
574574 summonExpr[Ordering [T ]] match {
575575 case Some (ord) => ' { new TreeSet [T ]()($ord) }
576576 case _ => ' { new HashSet [T ] }
@@ -587,7 +587,7 @@ inline method that can calculate either a value of type `Int` or a value of type
587587``` scala
588588inline def defaultOf (inline str : String ) <: Any = $ { defaultOfImpl(' str ) }
589589
590- def defaultOfImpl (strExpr : Expr [String ])(given QuoteContext ): Expr [Any ] =
590+ def defaultOfImpl (strExpr : Expr [String ])(using QuoteContext ): Expr [Any ] =
591591 strExpr.value match
592592 case " int" => ' {1 }
593593 case " string" => ' {" a" }
@@ -627,7 +627,7 @@ In `scala.quoted.matching` contains object that can help extract values from `Ex
627627These could be used in the following way to optimize any call to ` sum ` that has statically known values.
628628``` scala
629629inline 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 {
631631 case ConstSeq (args) => // args is of type Seq[Int]
632632 Expr (args.sum) // precompute result of sum
633633 case ExprSeq (argExprs) => // argExprs is of type Seq[Expr[Int]]
@@ -660,7 +660,7 @@ optimize {
660660``` scala
661661def sum (args : Int * ): Int = args.sum
662662inline 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 {
664664 // Match a call to sum without any arguments
665665 case ' { sum() } => Expr (0 )
666666 // 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
669669 case ' { sum($ {ExprSeq (args)}: _* ) } => sumExpr(args)
670670 case body => body
671671}
672- private def sumExpr (args1 : Seq [Expr [Int ]])(given QuoteContext ): Expr [Int ] = {
672+ private def sumExpr (args1 : Seq [Expr [Int ]])(using QuoteContext ): Expr [Int ] = {
673673 def flatSumArgs (arg : Expr [Int ]): Seq [Expr [Int ]] = arg match {
674674 case ' { sum($ {ExprSeq (subArgs)}: _* ) } => subArgs.flatMap(flatSumArgs)
675675 case arg => Seq (arg)
@@ -692,7 +692,7 @@ private def sumExpr(args1: Seq[Expr[Int]])(given QuoteContext): Expr[Int] = {
692692Sometimes it is necessary to get a more precise type for an expression. This can be achived using the following pattern match.
693693
694694``` scala
695- def f (exp : Expr [Any ])(given QuoteContext ) =
695+ def f (exp : Expr [Any ])(using QuoteContext ) =
696696 expr match
697697 case ' { $x : $t } =>
698698 // 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:
707707``` scala
708708inline def (sc : StringContext ).showMe(args : => Any * ): String = $ { showMeExpr(' sc , ' args ) }
709709
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 ] = {
711711 argsExpr match {
712712 case ExprSeq (argExprs) =>
713713 val argShowedExprs = argExprs.map {
0 commit comments