Skip to content

Commit 49e0fbf

Browse files
authored
Merge pull request #5562 from dotty-staging/fix-pcp-docs
Improve `showExpr` example
2 parents b1ce477 + 3924667 commit 49e0fbf

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ prints it again in an error message if it evaluates to `false`.
2929
inline def assert(expr: => Boolean): Unit =
3030
~ assertImpl('(expr))
3131

32-
def assertImpl(expr: Expr[Boolean]) =
33-
'{ if !(~expr) then throw new AssertionError(s"failed assertion: ${~showExpr(expr)}") }
32+
def assertImpl(expr: Expr[Boolean]) = '{
33+
if !(~expr) then
34+
throw new AssertionError(s"failed assertion: ${~showExpr(expr)}")
35+
}
36+
37+
def showExpr(expr: Expr[Boolean]): Expr[String] =
38+
'("<some source code>") // Better implementation later in this document
3439

3540
If `e` is an expression, then `'(e)` or `'{e}` represent the typed
3641
abstract syntax tree representing `e`. If `T` is a type, then `'[T]`
@@ -587,9 +592,12 @@ analogue of lifting.
587592

588593
Using lifting, we can now give the missing definition of `showExpr` in the introductory example:
589594

590-
def showExpr[T](expr: Expr[T]): Expr[String] = expr.toString
595+
def showExpr[T](expr: Expr[T]): Expr[String] = {
596+
val code = expr.show
597+
code.toExpr
598+
}
591599

592-
That is, the `showExpr` method converts its `Expr` argument to a string, and lifts
600+
That is, the `showExpr` method converts its `Expr` argument to a string (`code`), and lifts
593601
the result back to an `Expr[String]` using the implicit `toExpr` conversion.
594602

595603
## Implementation

0 commit comments

Comments
 (0)