Skip to content

Fix #7858: Update QuoteContext in Liftable documentation #7915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/docs/reference/metaprogramming/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ knowing anything about the representation of `Expr` trees. For
instance, here is a possible instance of `Liftable[Boolean]`:
```scala
given Liftable[Boolean] {
def toExpr(b: Boolean)(given QuoteContext): Expr[Boolean] =
def toExpr(b: Boolean) =
if (b) '{ true } else '{ false }
}
```
Expand All @@ -276,7 +276,7 @@ possible implementation of `Liftable[Int]` that does not use the underlying
tree machinery:
```scala
given Liftable[Int] {
def toExpr(n: Int)(given QuoteContext): Expr[Int] = n match {
def toExpr(n: Int) = n match {
case Int.MinValue => '{ Int.MinValue }
case _ if n < 0 => '{ - ${ toExpr(-n) } }
case 0 => '{ 0 }
Expand All @@ -288,9 +288,9 @@ given Liftable[Int] {
Since `Liftable` is a type class, its instances can be conditional. For example,
a `List` is liftable if its element type is:
```scala
given [T: Liftable] : Liftable[List[T]] {
def toExpr(xs: List[T])(given QuoteContext): Expr[List[T]] = xs match {
case head :: tail => '{ ${ toExpr(head) } :: ${ toExpr(tail) } }
given [T: Liftable : Type] : Liftable[List[T]] {
def toExpr(xs: List[T]) = xs match {
case head :: tail => '{ ${ Expr(head) } :: ${ toExpr(tail) } }
case Nil => '{ Nil: List[T] }
}
}
Expand All @@ -303,7 +303,7 @@ analogue of lifting.

Using lifting, we can now give the missing definition of `showExpr` in the introductory example:
```scala
def showExpr[T](expr: Expr[T]): Expr[String] = {
def showExpr[T](expr: Expr[T])(given QuoteContext): Expr[String] = {
val code: String = expr.show
Expr(code)
}
Expand Down
32 changes: 32 additions & 0 deletions tests/pos/macro-docs.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import scala.quoted._

object MacrosMD_Liftable {

given Liftable[Boolean] {
def toExpr(b: Boolean) =
if (b) '{ true } else '{ false }
}

given Liftable[Int] {
def toExpr(n: Int) = n match {
case Int.MinValue => '{ Int.MinValue }
case _ if n < 0 => '{ - ${ toExpr(-n) } }
case 0 => '{ 0 }
case _ if n % 2 == 0 => '{ ${ toExpr(n / 2) } * 2 }
case _ => '{ ${ toExpr(n / 2) } * 2 + 1 }
}
}

given [T: Liftable : Type] : Liftable[List[T]] {
def toExpr(xs: List[T]) = xs match {
case head :: tail => '{ ${ Expr(head) } :: ${ toExpr(tail) } }
case Nil => '{ Nil: List[T] }
}
}

def showExpr[T](expr: Expr[T])(given QuoteContext): Expr[String] = {
val code: String = expr.show
Expr(code)
}

}