Skip to content

Commit b75afac

Browse files
Merge pull request #7609 from dotty-staging/docs-macros
Some fix to macros docs (#7608)
2 parents 3016573 + ec1f012 commit b75afac

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

docs/docs/reference/metaprogramming/macros.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ import scala.quoted._
3636
inline def assert(expr: => Boolean): Unit =
3737
${ assertImpl('expr) }
3838

39-
def assertImpl(expr: Expr[Boolean]) = '{
39+
def assertImpl(expr: Expr[Boolean])(given QuoteContext) = '{
4040
if (!$expr)
4141
throw new AssertionError(s"failed assertion: ${${ showExpr(expr) }}")
4242
}
4343

44-
def showExpr(expr: Expr[Boolean]): Expr[String] =
44+
def showExpr(expr: Expr[Boolean])(given QuoteContext): Expr[String] =
4545
'{ "<some source code>" } // Better implementation later in this document
4646
```
4747

@@ -238,22 +238,23 @@ Running `compile(letExp, Map())` would yield the following Scala code:
238238
'{ val y = 3; (2 + y) + 4 }
239239
```
240240
The body of the first clause, `case Num(n) => Expr(n)`, looks suspicious. `n`
241-
is declared as an `Int`, yet it is converted to an `Expr[Int]` with `toExpr`.
241+
is declared as an `Int`, yet it is converted to an `Expr[Int]` with `Expr()`.
242242
Shouldn’t `n` be quoted? In fact this would not
243243
work since replacing `n` by `'n` in the clause would not be phase
244244
correct.
245245

246-
The `toExpr` extension method is defined in package `quoted`:
246+
The `Expr.apply` method is defined in package `quoted`:
247247
```scala
248248
package quoted
249249

250-
given {
251-
def (x: T) toExpr[T: Liftable](given QuoteContext): Expr[T] = summon[Liftable[T]].toExpr(x)
250+
object Expr {
251+
...
252+
def apply[T: Liftable](x: T)(given QuoteContext): Expr[T] = summon[Liftable[T]].toExpr(x)
252253
...
253254
}
254255
```
255-
The extension says that values of types implementing the `Liftable` type class can be
256-
converted ("lifted") to `Expr` values using `toExpr`, provided a given import of `scala.quoted._` is in scope.
256+
This method says that values of types implementing the `Liftable` type class can be
257+
converted ("lifted") to `Expr` values using `Expr.apply`.
257258

258259
Dotty comes with given instances of `Liftable` for
259260
several types including `Boolean`, `String`, and all primitive number

0 commit comments

Comments
 (0)