Skip to content

Commit 3b86157

Browse files
committed
Fixes to the assertImpl example
Make sure `assertImpl` prints the tested expression rather than its result.
1 parent 043b5fd commit 3b86157

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

docs/docs/reference/symmetric-meta-programming.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ prints it again in an error message if it evaluates to `false`.
3232
~ assertImpl(’(expr))
3333

3434
def assertImpl(expr: Expr[Boolean]) =
35-
’{ if !(~expr) then throw new AssertionError(s"failed assertion: ${~expr.toString}") }
36-
35+
’{ if !(~expr) then throw new AssertionError(s"failed assertion: ${~showExpr(expr)}") }
3736

3837
If `e` is an expression, then `’(e)` or `’{e}` represent the typed
3938
abstract syntax tree representing `e`. If `T` is a type, then `’[T]`
@@ -531,15 +530,12 @@ In the end, `Liftable` resembles very much a serialization
531530
framework. Like the latter it can be derived systematically for all
532531
collections, case classes and enums.
533532

534-
In fact, the initial example of assertions also uses a lifting conversion under the hood.
535-
Recall the failure clause:
533+
Using lifting, we can now give the missing definition of `showExpr` in the introductory example:
536534

537-
throw new AssertionError(s"failed assertion: ${~expr.toString}") }
535+
def showExpr[T](expr: Expr[T]): Expr[String] = expr.toString
538536

539-
Here, `expr.toString` yields `expr`'s representation in String form. That string
540-
is lifted to an `Expr[String]` since the required type of a splice argument is an `Expr`.
541-
The lifted result is then spliced in into the `AssertionError` argument, giving
542-
back again the original string representation of `expr`.
537+
That is, the `showExpr` method converts its `Expr` argument to a string, and lifts
538+
the result back to an `Expr[String]` using the implicit `toExpr` conversion.
543539

544540
## Implementation
545541

library/src/scala/quoted/Liftable.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ abstract class Liftable[T] {
1515
object Liftable {
1616
implicit def IntIsLiftable: Liftable[Int] = ???
1717
implicit def BooleanIsLiftable: Liftable[Boolean] = ???
18+
implicit def StringIsLiftable: Liftable[String] = ???
1819
}

tests/pos/quoted.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ class Test {
88
~ assertImpl('(expr))
99

1010
def assertImpl(expr: Expr[Boolean]) =
11-
'{ if !(~expr) then throw new AssertionError(s"failed assertion: ${~expr}") }
11+
'{ if !(~expr) then throw new AssertionError(s"failed assertion: ${~showExpr(expr)}") }
12+
13+
def showExpr[T](expr: Expr[T]): Expr[String] = expr.toString
1214

1315
inline def power(inline n: Int, x: Double) = ~powerCode(n, '(x))
1416

0 commit comments

Comments
 (0)