@@ -208,14 +208,14 @@ reference to a type `T` in subsequent phases to a type-splice, by rewriting `T`
208
208
For instance, the user-level definition of ` to ` :
209
209
210
210
``` scala
211
- def to [T , R ](f : Expr [T ] => Expr [R ])(using t : Type [T ], r : Type [R ], Quotes ): Expr [T => R ] =
211
+ def to [T , R ](f : Expr [T ] => Expr [R ])(using t : Type [T ], r : Type [R ])( using Quotes ): Expr [T => R ] =
212
212
' { (x : T ) => $ { f(' x ) } }
213
213
```
214
214
215
215
would be rewritten to
216
216
217
217
``` scala
218
- def to [T , R ](f : Expr [T ] => Expr [R ])(using t : Type [T ], r : Type [R ], Quotes ): Expr [T => R ] =
218
+ def to [T , R ](f : Expr [T ] => Expr [R ])(using t : Type [T ], r : Type [R ])( using Quotes ): Expr [T => R ] =
219
219
' { (x : t.Underlying ) => $ { f(' x ) } }
220
220
```
221
221
@@ -239,6 +239,7 @@ enum Exp {
239
239
case Var (x : String )
240
240
case Let (x : String , e : Exp , in : Exp )
241
241
}
242
+ import Exp ._
242
243
```
243
244
244
245
The interpreted language consists of numbers ` Num ` , addition ` Plus ` , and variables
@@ -479,7 +480,7 @@ inline def power(x: Double, inline n: Int) = ${ powerCode('x, 'n) }
479
480
private def powerCode (x : Expr [Double ], n : Expr [Int ])(using Quotes ): Expr [Double ] =
480
481
n.value match
481
482
case Some (m) => powerCode(x, m)
482
- case None => ' { Math .pow($x, $y ) }
483
+ case None => ' { Math .pow($x, $n.toDouble ) }
483
484
484
485
private def powerCode (x : Expr [Double ], n : Int )(using Quotes ): Expr [Double ] =
485
486
if (n == 0 ) ' { 1.0 }
@@ -621,6 +622,7 @@ Similarly to the `summonFrom` construct, it is possible to make implicit search
621
622
in a quote context. For this we simply provide ` scala.quoted.Expr.summon ` :
622
623
623
624
``` scala
625
+ import scala .collection .immutable .{ TreeSet , HashSet }
624
626
inline def setFor [T ]: Set [T ] = $ { setForExpr[T ] }
625
627
626
628
def setForExpr [T : Type ](using Quotes ): Expr [Set [T ]] = {
@@ -770,11 +772,17 @@ private def showMeExpr(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using
770
772
trait Show [- T ] {
771
773
def show (x : T ): String
772
774
}
775
+ // in a different file
776
+ given Show [Boolean ] {
777
+ def show (b : Boolean ) = " boolean!"
778
+ }
779
+
780
+ println(showMe " ${true }" )
773
781
```
774
782
775
783
### Open code patterns
776
784
777
- Quote pattern matching also provides higher-order patterns to match open terms. If a quoted term contains a definition,
785
+ Quoted pattern matching also provides higher-order patterns to match open terms. If a quoted term contains a definition,
778
786
then the rest of the quote can refer to this definition.
779
787
780
788
``` scala
0 commit comments