@@ -65,12 +65,14 @@ is treated as a quote `'{x}`. See the Syntax section below for details.
65
65
66
66
Quotes and splices are duals of each other. For arbitrary
67
67
expressions ` e ` and types ` T ` we have:
68
- ```
68
+
69
+ ``` scala
69
70
$ {' {e}} = e
70
71
' {$ {e}} = e
71
72
$ {' [T ]} = T
72
73
' [$ {T }] = T
73
74
```
75
+
74
76
### Types for Quotations
75
77
76
78
The type signatures of quotes and splices can be described using
@@ -202,11 +204,14 @@ For instance, the user-level definition of `to`:
202
204
def to [T , R ](f : Expr [T ] => Expr [R ])(using t : Type [T ], r : Type [R ], Quotes ): Expr [T => R ] =
203
205
' { (x : T ) => $ { f(' x ) } }
204
206
```
207
+
205
208
would be rewritten to
209
+
206
210
``` scala
207
211
def to [T , R ](f : Expr [T ] => Expr [R ])(using t : Type [T ], r : Type [R ], Quotes ): Expr [T => R ] =
208
- ' { (x : t.Underlying } ) => $ { f(' x ) } }
212
+ ' { (x : t.Underlying ) => $ { f(' x ) } }
209
213
```
214
+
210
215
The ` summon ` query succeeds because there is a given instance of
211
216
type ` Type[T] ` available (namely the given parameter corresponding
212
217
to the context bound ` : Type ` ), and the reference to that value is
@@ -328,7 +333,6 @@ def showExpr[T](expr: Expr[T])(using Quotes): Expr[String] = {
328
333
That is, the ` showExpr ` method converts its ` Expr ` argument to a string (` code ` ), and lifts
329
334
the result back to an ` Expr[String] ` using ` Expr.apply ` .
330
335
331
-
332
336
### Lifting Types
333
337
334
338
The previous section has shown that the metaprogramming framework has
@@ -612,6 +616,7 @@ val b: String = defaultOf("string")
612
616
```
613
617
614
618
### Defining a macro and using it in a single project
619
+
615
620
It is possible to define macros and use them in the same project as long as the implementation
616
621
of the macros does not have run-time dependencies on code in the file where it is used.
617
622
It might still have compile-time dependencies on types and quoted code that refers to the use-site file.
@@ -622,7 +627,6 @@ If there are any suspended files when the compilation ends, the compiler will au
622
627
compilation of the suspended files using the output of the previous (partial) compilation as macro classpath.
623
628
In case all files are suspended due to cyclic dependencies the compilation will fail with an error.
624
629
625
-
626
630
### Pattern matching on quoted expressions
627
631
628
632
It is possible to deconstruct or extract values out of ` Expr ` using pattern matching.
@@ -712,7 +716,6 @@ def f(exp: Expr[Any])(using Quotes) =
712
716
713
717
This might be used to then perform an implicit search as in:
714
718
715
-
716
719
``` scala
717
720
extension (inline sc : StringContext ) inline def showMe (inline args : Any * ): String = $ { showMeExpr(' sc , ' args ) }
718
721
@@ -744,7 +747,8 @@ trait Show[-T] {
744
747
745
748
Quote pattern matching also provides higher-order patterns to match open terms. If a quoted term contains a definition,
746
749
then the rest of the quote can refer to this definition.
747
- ```
750
+
751
+ ``` scala
748
752
' {
749
753
val x : Int = 4
750
754
x * x
@@ -785,6 +789,6 @@ eval { // expands to the code: (16: Int)
785
789
We can also close over several bindings using ` $b(a1, a2, ..., an) ` .
786
790
To match an actual application we can use braces on the function part ` ${b}(a1, a2, ..., an) ` .
787
791
788
-
789
792
### More details
793
+
790
794
[ More details] ( ./macros-spec.md )
0 commit comments