Skip to content

Commit f4db9f7

Browse files
committed
Update metaprogramming docs to new syntax
1 parent 3c18f6b commit f4db9f7

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

docs/docs/reference/metaprogramming/macros.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ reference to a type `T` in subsequent phases to a type-splice, by rewriting `T`
208208
For instance, the user-level definition of `to`:
209209

210210
```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] =
212212
'{ (x: T) => ${ f('x) } }
213213
```
214214

215215
would be rewritten to
216216

217217
```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] =
219219
'{ (x: t.Underlying) => ${ f('x) } }
220220
```
221221

@@ -239,6 +239,7 @@ enum Exp {
239239
case Var(x: String)
240240
case Let(x: String, e: Exp, in: Exp)
241241
}
242+
import Exp._
242243
```
243244

244245
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) }
479480
private def powerCode(x: Expr[Double], n: Expr[Int])(using Quotes): Expr[Double] =
480481
n.value match
481482
case Some(m) => powerCode(x, m)
482-
case None => '{ Math.pow($x, $y) }
483+
case None => '{ Math.pow($x, $n.toDouble) }
483484

484485
private def powerCode(x: Expr[Double], n: Int)(using Quotes): Expr[Double] =
485486
if (n == 0) '{ 1.0 }
@@ -621,6 +622,7 @@ Similarly to the `summonFrom` construct, it is possible to make implicit search
621622
in a quote context. For this we simply provide `scala.quoted.Expr.summon`:
622623

623624
```scala
625+
import scala.collection.immutable.{ TreeSet, HashSet }
624626
inline def setFor[T]: Set[T] = ${ setForExpr[T] }
625627

626628
def setForExpr[T: Type](using Quotes): Expr[Set[T]] = {
@@ -770,11 +772,17 @@ private def showMeExpr(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(using
770772
trait Show[-T] {
771773
def show(x: T): String
772774
}
775+
// in a different file
776+
given Show[Boolean] {
777+
def show(b: Boolean) = "boolean!"
778+
}
779+
780+
println(showMe"${true}")
773781
```
774782

775783
### Open code patterns
776784

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,
778786
then the rest of the quote can refer to this definition.
779787

780788
```scala

docs/docs/reference/metaprogramming/staging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ It will create a project with the necessary dependencies and some examples.
8484
In case you prefer to create the project on your own, make sure to define the following dependency in your build.sbt
8585

8686
```scala
87-
libraryDependencies += "ch.epfl.lamp" %% "scala3-staging" % scalaVersion.value
87+
libraryDependencies += "org.scala-lang" %% "scala3-staging" % scalaVersion.value
8888
```
8989

9090
and in case you use `scalac`/`scala` directly, then use the `-with-compiler` flag for both:

docs/docs/reference/metaprogramming/tasty-reflect.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def natConstImpl(x: Expr[Int])(using Quotes): Expr[Int] = {
4444
import quotes.reflect._
4545
val xTree: Term = x.asTerm
4646
xTree match {
47-
case Inlined(_, _, Literal(Constant(n: Int))) =>
47+
case Inlined(_, _, Literal(Constant.Int(n))) =>
4848
if (n <= 0) {
4949
report.error("Parameter must be natural number")
5050
'{0}
@@ -131,4 +131,3 @@ def lets(terms: List[Term])(body: List[Term] => Term): Term = ...
131131
## More Examples
132132

133133
* Start experimenting with TASTy Reflect ([link](https://github.com/nicolasstucki/tasty-reflection-exercise))
134-

0 commit comments

Comments
 (0)