Skip to content

Commit 53e6d67

Browse files
committed
Fixes to meta-programming doc
1 parent b0de920 commit 53e6d67

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +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}") }
35+
’{ if !(~expr) then throw new AssertionError(s"failed assertion: ${~expr.toString}") }
3636

3737

3838
If `e` is an expression, then `’(e)` or `’{e}` represent the typed
@@ -490,14 +490,14 @@ is defined in the companion object of class `Expr` as follows:
490490
The conversion says that values of types implementing the `Liftable`
491491
type class can be converted ("lifted") automatically to `Expr`
492492
values. Dotty comes with instance definitions of `Liftable` for
493-
several types including all underlying types of literals. For example,
494-
`Int` values can be converted to `Expr[Int]` values by wrapping the
495-
value in a `Literal` tree node. This makes use of the underlying tree
496-
representation in the compiler for efficiency. But the `Liftable`
497-
instances are nevertheless not "magic" in the sense that they could
498-
all be defined in a user program without knowing anything about the
499-
representation of `Expr` trees. For instance, here is a possible
500-
instance of `Liftable[Boolean]`:
493+
several types including `Boolean`, `String`, and all primitive number
494+
types. For example, `Int` values can be converted to `Expr[Int]`
495+
values by wrapping the value in a `Literal` tree node. This makes use
496+
of the underlying tree representation in the compiler for
497+
efficiency. But the `Liftable` instances are nevertheless not "magic"
498+
in the sense that they could all be defined in a user program without
499+
knowing anything about the representation of `Expr` trees. For
500+
instance, here is a possible instance of `Liftable[Boolean]`:
501501

502502
implicit def BooleanIsLiftable: Liftable[Boolean] = new {
503503
implicit def toExpr(b: Boolean) = if (b) ’(true) else ’(false)
@@ -531,6 +531,16 @@ In the end, `Liftable` resembles very much a serialization
531531
framework. Like the latter it can be derived systematically for all
532532
collections, case classes and enums.
533533

534+
In fact, the initial example of assertions also uses a lifting conversion under the hood.
535+
Recall the failure clause:
536+
537+
throw new AssertionError(s"failed assertion: ${~expr.toString}") }
538+
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`.
543+
534544
## Implementation
535545

536546
### Syntax changes
@@ -602,9 +612,9 @@ The syntax of terms, values, and types is given as follows:
602612
~t splice
603613

604614
Values v ::= (x: T) => t lambda
605-
q pure quote
615+
u quote
606616

607-
Quoted q ::= x | (x: T) => q | q q | ’t
617+
Simple terms u ::= x | (x: T) => u | u u | ’t
608618

609619
Types T ::= A base type
610620
T -> T function type
@@ -634,7 +644,7 @@ We define a small step reduction relation `-->` with the following rules:
634644

635645
((x: T) => t) v --> [x := v]t
636646

637-
~(’t) --> t
647+
~(’u) --> u
638648

639649
t1 --> t2
640650
-----------------
@@ -647,7 +657,7 @@ position of an evaluation context. Evaluation contexts `e` and
647657
splice evaluation context `e_s` are defined syntactically as follows:
648658

649659
Eval context e ::= [ ] | e t | v e | ’e_s[~e]
650-
Splice context e_s ::= [ ] | (x: T) => e_s | e_s t | q e_s
660+
Splice context e_s ::= [ ] | (x: T) => e_s | e_s t | u e_s
651661

652662
### Typing rules
653663

0 commit comments

Comments
 (0)