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
Copy file name to clipboardExpand all lines: docs/docs/reference/metaprogramming/macros.md
+46Lines changed: 46 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -744,5 +744,51 @@ trait Show[-T] {
744
744
}
745
745
```
746
746
747
+
#### Open code patterns
748
+
749
+
Quote pattern matching also provides higher-order patterns to match open terms. If a quoted term contains a definition,
750
+
then the rest of the quote can refer to this definition.
751
+
```
752
+
'{
753
+
val x: Int = 4
754
+
x * x
755
+
}
756
+
```
757
+
758
+
To match such a term we need to match the definition and the rest of the code, but we need to expicilty state that the rest of the code may refer to this definition.
759
+
```scala
760
+
case'{ valy:Int= $x; $body(y):Int } =>
761
+
```
762
+
Here `$x` will match any closed expression while `$body(y)` will match expression that is closed under `y`. Then
763
+
the subxpression of type `Expr[Int]` is bound to `body` as an `Expr[Int => Int]`. The extra argument represents the references to `y`. Usually this expression is used in compination with `Expr.betaReduce` to replace the extra argument.
0 commit comments