@@ -32,8 +32,7 @@ prints it again in an error message if it evaluates to `false`.
32
32
~ assertImpl(’(expr))
33
33
34
34
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)}") }
37
36
38
37
If ` e ` is an expression, then ` ’(e) ` or ` ’{e} ` represent the typed
39
38
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
531
530
framework. Like the latter it can be derived systematically for all
532
531
collections, case classes and enums.
533
532
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:
536
534
537
- throw new AssertionError(s"failed assertion: ${~ expr.toString}") }
535
+ def showExpr[T](expr: Expr[T]): Expr[String] = expr.toString
538
536
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.
543
539
544
540
## Implementation
545
541
0 commit comments