@@ -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] `
@@ -532,15 +531,12 @@ In the end, `Liftable` resembles very much a serialization
532
531
framework. Like the latter it can be derived systematically for all
533
532
collections, case classes and enums.
534
533
535
- In fact, the initial example of assertions also uses a lifting conversion under the hood.
536
- Recall the failure clause:
534
+ Using lifting, we can now give the missing definition of ` showExpr ` in the introductory example:
537
535
538
- throw new AssertionError(s"failed assertion: ${~ expr.toString}") }
536
+ def showExpr[T](expr: Expr[T]): Expr[String] = expr.toString
539
537
540
- Here, ` expr.toString ` yields ` expr ` 's representation in String form. That string
541
- is lifted to an ` Expr[String] ` since the required type of a splice argument is an ` Expr ` .
542
- The lifted result is then spliced in into the ` AssertionError ` argument, giving
543
- back again the original string representation of ` expr ` .
538
+ That is, the ` showExpr ` method converts its ` Expr ` argument to a string, and lifts
539
+ the result back to an ` Expr[String] ` using the implicit ` toExpr ` conversion.
544
540
545
541
## Implementation
546
542
0 commit comments