Skip to content

Commit 7d5280e

Browse files
committed
Fix #7858: Update QuoteContext in Liftable documentation
1 parent 4fd0520 commit 7d5280e

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

docs/docs/reference/metaprogramming/macros.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ knowing anything about the representation of `Expr` trees. For
267267
instance, here is a possible instance of `Liftable[Boolean]`:
268268
```scala
269269
given Liftable[Boolean] {
270-
def toExpr(b: Boolean)(given QuoteContext): Expr[Boolean] =
270+
def toExpr(b: Boolean) =
271271
if (b) '{ true } else '{ false }
272272
}
273273
```
@@ -276,7 +276,7 @@ possible implementation of `Liftable[Int]` that does not use the underlying
276276
tree machinery:
277277
```scala
278278
given Liftable[Int] {
279-
def toExpr(n: Int)(given QuoteContext): Expr[Int] = n match {
279+
def toExpr(n: Int) = n match {
280280
case Int.MinValue => '{ Int.MinValue }
281281
case _ if n < 0 => '{ - ${ toExpr(-n) } }
282282
case 0 => '{ 0 }
@@ -288,9 +288,9 @@ given Liftable[Int] {
288288
Since `Liftable` is a type class, its instances can be conditional. For example,
289289
a `List` is liftable if its element type is:
290290
```scala
291-
given [T: Liftable] : Liftable[List[T]] {
292-
def toExpr(xs: List[T])(given QuoteContext): Expr[List[T]] = xs match {
293-
case head :: tail => '{ ${ toExpr(head) } :: ${ toExpr(tail) } }
291+
given [T: Liftable : Type] : Liftable[List[T]] {
292+
def toExpr(xs: List[T]) = xs match {
293+
case head :: tail => '{ ${ Expr(head) } :: ${ toExpr(tail) } }
294294
case Nil => '{ Nil: List[T] }
295295
}
296296
}
@@ -303,7 +303,7 @@ analogue of lifting.
303303

304304
Using lifting, we can now give the missing definition of `showExpr` in the introductory example:
305305
```scala
306-
def showExpr[T](expr: Expr[T]): Expr[String] = {
306+
def showExpr[T](expr: Expr[T])(given QuoteContext): Expr[String] = {
307307
val code: String = expr.show
308308
Expr(code)
309309
}

tests/pos/macro-docs.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import scala.quoted._
2+
3+
object MacrosMD_Liftable {
4+
5+
given Liftable[Boolean] {
6+
def toExpr(b: Boolean) =
7+
if (b) '{ true } else '{ false }
8+
}
9+
10+
given Liftable[Int] {
11+
def toExpr(n: Int) = n match {
12+
case Int.MinValue => '{ Int.MinValue }
13+
case _ if n < 0 => '{ - ${ toExpr(-n) } }
14+
case 0 => '{ 0 }
15+
case _ if n % 2 == 0 => '{ ${ toExpr(n / 2) } * 2 }
16+
case _ => '{ ${ toExpr(n / 2) } * 2 + 1 }
17+
}
18+
}
19+
20+
given [T: Liftable : Type] : Liftable[List[T]] {
21+
def toExpr(xs: List[T]) = xs match {
22+
case head :: tail => '{ ${ Expr(head) } :: ${ toExpr(tail) } }
23+
case Nil => '{ Nil: List[T] }
24+
}
25+
}
26+
27+
def showExpr[T](expr: Expr[T])(given QuoteContext): Expr[String] = {
28+
val code: String = expr.show
29+
Expr(code)
30+
}
31+
32+
}

0 commit comments

Comments
 (0)