Skip to content

Commit 036809f

Browse files
committed
wip (address comments)
1 parent eb09f7f commit 036809f

File tree

6 files changed

+69
-44
lines changed

6 files changed

+69
-44
lines changed

docs/docs/reference/metaprogramming/inline.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,17 @@ final val two = toInt[Succ[Succ[Zero]]]
367367
behavior. Since `toInt` performs static checks over the static type of `N` we
368368
can safely use it to scrutinize its return type (`S[S[Z]]` in this case).
369369

370+
##### Error
371+
372+
This package provides a compile time `error` definition with the following signature:
373+
374+
```scala
375+
inline def error(inline msg: String, objs: Any*): Nothing
376+
```
377+
378+
The purpose of this is to expand at the point of use, an error message (a
379+
constant string) and append with commas, compile time values passed in the
380+
`objs` param.
370381

371382
#### Implicit Match
372383

docs/docs/reference/metaprogramming/macros-spec.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ a quoted expression or type is treated as a splice `${x}` and a quoted identifie
2323

2424
### Implementation in `dotc`
2525

26-
Quotes and splices are primitive forms in the generated abstract
27-
syntax trees. They are eliminated in an expansion phase
28-
`Staging`. This phase runs after typing and pickling.
26+
Quotes and splices are primitive forms in the generated abstract syntax trees.
27+
Top-level splices are eliminated during macro expansion while typing. On the
28+
other hand, top-level quotes are eliminated in an expansion phase `ReifyQuotes`
29+
phase (after typing and pickling). PCP checking occurs while preparing the RHS
30+
of an inline method for top-level splices and in the `Staging` phase (after
31+
typing and before pickling).
2932

3033
Macro-expansion works outside-in. If the outermost scope is a splice,
3134
the spliced AST will be evaluated in an interpreter. A call to a

docs/docs/reference/metaprogramming/macros.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,33 @@ layout: doc-page
33
title: "Macros"
44
---
55

6-
<!--
7-
Example introducing macros earlier, e.g.,
8-
http://dotty.epfl.ch/docs/reference/other-new-features/principled-meta-programming.html#relationship-with-inline-and-macros
9-
10-
11-
- Syntax (example through string interpolation)
12-
- Expr and Type
13-
- PCP
14-
- Quoted Functions and β-reduction
15-
- Types and PCP (e.g., healing)
16-
- Lifting
17-
- Expressions (http://dotty.epfl.ch/docs/reference/other-new-features/principled-meta-programming.html#the-liftable-type-class)
18-
- Types (http://dotty.epfl.ch/docs/reference/other-new-features/principled-meta-programming.html#lifting-types)
19-
- Scope Extrusion (needs new name)
20-
http://dotty.epfl.ch/docs/reference/other-new-features/principled-meta-programming.html#limitations-to-splicing
21-
- var references (e.g., let)
22-
- More Details include: spec syntax, formalization -->
23-
246
### Macros: Quotes and Splices
257

268
Macros are built on two well-known fundamental operations: quotation and
279
splicing. Quotation is expressed as `'{...}` for expressions (both forms are
2810
equivalent) and as `'[...]` for types. Splicing is expressed as `${ ... }`.
11+
Additionally, within a quote or a splice we can quote or splice identifiers
12+
directly (i.e. `'e` and `$e`). Readers may notice the resemblance of the two
13+
aforementioned syntactic schemes with the familiar string interpolation syntax.
14+
15+
```scala
16+
println(s"Hello, $name, here is the result of 1 + 1 = ${1 + 1}")
17+
```
2918

30-
For example, the code below presents an inline function `assert`
31-
which calls at compile-time a method `assertImpl` with a boolean
32-
expression tree as argument. `assertImpl` evaluates the expression and
19+
In string interpolation we _quoted_ a string and then we _spliced_ into it, two
20+
others. The first, `name`, is a reference to a value of type `string`, and the
21+
second is an arithmetic expression that will be _evaluated_ followed by the
22+
splicing of its string representation.
23+
24+
Quotes and splices in this section allow us to treat code in a similar way,
25+
effectively supporting macros. The entry point for macros is an inline method
26+
with a top-level splice. We call it a top-level because it is the only occation
27+
where we encounter a splice outside a quote (consider as a quote the
28+
compilation-unit at the call-site). For example, the code below presents an
29+
`inline` method `assert` which calls at compile-time a method `assertImpl` with
30+
a boolean expression tree as argument. `assertImpl` evaluates the expression and
3331
prints it again in an error message if it evaluates to `false`.
32+
3433
```scala
3534
import scala.quoted._
3635

@@ -45,6 +44,7 @@ prints it again in an error message if it evaluates to `false`.
4544
def showExpr(expr: Expr[Boolean]): Expr[String] =
4645
'{ "<some source code>" } // Better implementation later in this document
4746
```
47+
4848
If `e` is an expression, then `'{e}` represent the typed
4949
abstract syntax tree representing `e`. If `T` is a type, then `'[T]`
5050
represents the type structure representing `T`. The precise
@@ -264,7 +264,7 @@ several types including `Boolean`, `String`, and all primitive number
264264
types. For example, `Int` values can be converted to `Expr[Int]`
265265
values by wrapping the value in a `Literal` tree node. This makes use
266266
of the underlying tree representation in the compiler for
267-
efficiency. But the `Liftable` instances are nevertheless not "magic"
267+
efficiency. But the `Liftable` instances are nevertheless not _magic_
268268
in the sense that they could all be defined in a user program without
269269
knowing anything about the representation of `Expr` trees. For
270270
instance, here is a possible instance of `Liftable[Boolean]`:
@@ -292,26 +292,26 @@ a `List` is liftable if its element type is:
292292
```scala
293293
implied [T: Liftable] for Liftable[List[T]] {
294294
def toExpr(xs: List[T]): Expr[List[T]] = xs match {
295-
case x :: xs1 => '{ ${ toExpr(x) } :: ${ toExpr(xs1) } }
295+
case head :: tail => '{ ${ toExpr(head) } :: ${ toExpr(tail) } }
296296
case Nil => '{ Nil: List[T] }
297297
}
298298
}
299299
```
300300
In the end, `Liftable` resembles very much a serialization
301301
framework. Like the latter it can be derived systematically for all
302302
collections, case classes and enums. Note also that the synthesis
303-
of "type-tag" values of type `Type[T]` is essentially the type-level
303+
of _type-tag_ values of type `Type[T]` is essentially the type-level
304304
analogue of lifting.
305305

306306
Using lifting, we can now give the missing definition of `showExpr` in the introductory example:
307307
```scala
308308
def showExpr[T](expr: Expr[T]): Expr[String] = {
309-
val code = expr.show
309+
val code: String = expr.show
310310
code.toExpr
311311
}
312312
```
313313
That is, the `showExpr` method converts its `Expr` argument to a string (`code`), and lifts
314-
the result back to an `Expr[String]` using the `toExpr` wrapper.
314+
the result back to an `Expr[String]` using the `toExpr` method.
315315

316316
**Note**: the `toExpr` extension method can be ommited by importing an implicit
317317
conversion with `import scala.quoted.autolift._`. The programmer is able to
@@ -451,12 +451,12 @@ soundness: code in splices must be free of side effects. The restriction
451451
prevents code like this:
452452

453453
```scala
454-
var x: Expr[T]
454+
var x: Expr[T] = ...
455455
'{ (y: T) => ${ x = 'y; 1 } }
456456
```
457457

458-
This code, if it was accepted, would "extrude" a reference to a quoted variable
459-
`y` from its scope. This means we an subsequently access a variable outside the
458+
This code, if it was accepted, would _extrude_ a reference to a quoted variable
459+
`y` from its scope. This would subsequently allow access to a variable outside the
460460
scope where it is defined, which is likely problematic. The code is clearly
461461
phase consistent, so we cannot use PCP to rule it out. Instead we postulate a
462462
future effect system that can guarantee that splices are pure. In the absence of

docs/docs/reference/metaprogramming/tasty-inspect.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ layout: doc-page
33
title: "TASTy Inspection"
44
---
55

6+
TASTy files contain the full typed tree of a class including source positions
7+
and documentation. This is ideal for tools that analyze or extract semantic
8+
information of the code. To avoid the hassle of working directly with the TASTy
9+
file we provide the `TastyConsumer` which loads the contents and exposes it
10+
through the TASTy reflect API.
11+
12+
613
## Inspecting TASTy files
714

8-
To inspect the TASTy Reflect trees of a TASTy file a consumer can be defined in the following way.
15+
To inspect the TASTy Reflect trees of a TASTy file a consumer can be defined in
16+
the following way.
917

1018
```scala
1119
class Consumer extends TastyConsumer {
@@ -16,7 +24,8 @@ class Consumer extends TastyConsumer {
1624
}
1725
```
1826

19-
Then the consumer can be instantiated with the following code to get the tree of the class `foo.Bar` for a foo in the classpath.
27+
Then the consumer can be instantiated with the following code to get the tree of
28+
the class `foo.Bar` for a foo in the classpath.
2029

2130
```scala
2231
object Test {

docs/docs/reference/metaprogramming/tasty-reflect.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ You may find all you need without using TASTy Reflect.
1313

1414
## API: From quotes and splices to TASTy reflect trees and back
1515

16-
`quoted.Expr` and `quoted.Type` are only meant for generative meta-programming,
17-
generation of code without inspecting the ASTs. [Macros](./macros.html) provides
18-
the guarantee that the generation of code will be type-correct. Using TASTy
19-
Reflect will break these guarantees and may fail at macro expansion time, hence
20-
additional explicit checks must be done.
16+
With `quoted.Expr` and `quoted.Type` we can compute code but also analyze code
17+
by inspecting the ASTs. [Macros](./macros.html) provides the guarantee that the
18+
generation of code will be type-correct. Using TASTy Reflect will break these
19+
guarantees and may fail at macro expansion time, hence additional explicit
20+
checks must be done.
2121

2222
To provide reflection capabilities in macros we need to add an implicit
2323
parameter of type `scala.tasty.Reflection` and import it in the scope where it
@@ -41,7 +41,7 @@ def natConstImpl(x: Expr[Int])(implicit reflection: Reflection): Expr[Int] = {
4141
and `quoted.Type` which returns a `reflection.Term` that represents the tree of
4242
the expression and `reflection.TypeTree` that represents the tree of the type
4343
respectively. It will also import all extractors and methods on TASTy Reflect
44-
trees. For example the `Term.Literal(_)` extractor used below.
44+
trees. For example the `Literal(_)` extractor used below.
4545

4646
```scala
4747
def natConstImpl(x: Expr[Int])(implicit reflection: Reflection): Expr[Int] = {

docs/docs/reference/metaprogramming/toc.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ introduce the following fundamental facilities:
3737
the program in many phases or ... stages, thus the "Multi-Stage" in the
3838
programming paradigm we say it supports.
3939

40-
4. [TASTy Inspection](./tasty-inspect.html) Up until now we described how we can
40+
4. [TASTy Reflection](./tasty-reflect.html) With TASTy reflection we can
41+
`unseal` fragments of code and analyze them with reflection over the TASTy
42+
format of the code.
43+
44+
5. [TASTy Inspection](./tasty-inspect.html) Up until now we described how we can
4145
expand, calculate at compile-time, or generate programs. The Scala compiler
4246
offers quarantees at the level of types that the generated programs cannot go
4347
wrong. With TASTy inspection we can load compiled files and analyze their
4448
typed AST structure according to the TASTy format.
4549

46-
5. [TASTy Reflection](./tasty-reflect.html) With TASTy reflection we can
47-
`unseal` fragments of code and analyze them with reflection over the TASTy
48-
format of the code.
50+

0 commit comments

Comments
 (0)