Skip to content

Commit 35d60c0

Browse files
Merge pull request #2121 from Zeimyth/main
Grammar and Cleanup in Scala 3 Macro Documentation
2 parents ab81c1b + 665c826 commit 35d60c0

File tree

4 files changed

+162
-162
lines changed

4 files changed

+162
-162
lines changed

_overviews/scala3-macros/best-practices.md

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ num: 8
66
## Inline
77

88
### Be careful when inlining for performance
9-
To take the most advantage of the JVM JIT optimisations you want to avoid generating large methods.
9+
To take the most advantage of the JVM JIT optimisations, you want to avoid generating large methods.
1010

1111

1212
## Macros
@@ -16,24 +16,24 @@ To take the most advantage of the JVM JIT optimisations you want to avoid genera
1616
## Quoted code
1717

1818
### Keep quotes readable
19-
* Try to avoid `${..}` with arbitrary expressions inside
19+
* Try to avoid `${...}` with arbitrary expressions inside
2020
* Use `$someExpr`
2121
* Use `${ someExprFrom('localExpr) }`
2222

2323
To illustrate, consider the following example:
2424
```scala
25-
val x: StringContext = ...
26-
'{ StringContext(${Varargs(stringContext.parts.map(Expr(_)))}: _*) }
25+
val sc: StringContext = ...
26+
'{ StringContext(${Varargs(sc.parts.map(Expr(_)))}: _*) }
2727
```
28-
Instead we can write the following:
28+
Instead, we can write the following:
2929

3030
```scala
31-
val x: StringContext = ...
32-
val partExprs = stringContext.parts.map(Expr(_))
31+
val sc: StringContext = ...
32+
val partExprs = sc.parts.map(Expr(_))
3333
val partsExpr = Varargs(partExprs)
3434
'{ StringContext($partsExpr: _*) }
3535
```
36-
The contents of the quote are cleared this way.
36+
The contents of the quote are much more clear in the second example.
3737

3838
### Avoid nested contexts
3939

@@ -74,34 +74,29 @@ val leafSym: Symbol = leafTpe.typeSymbol
7474

7575
### Avoid `Symbol.tree`
7676

77-
On an object `sym: Symbol`, `sym.tree` returns the `Tree` associated to the
78-
symbol. Be careful when using this method as the tree for a symbol might not be
79-
defined. When the code associated to the symbol is defined in a different
80-
moment than this access, if the `-Yretain-trees` compilation option is not
81-
used, then the `tree` of the symbol will not be available. Symbols originated
82-
from Java code do not have an associated `tree`.
77+
On an object `sym: Symbol`, `sym.tree` returns the `Tree` associated with the symbol.
78+
Be careful when using this method, as the tree for a symbol might not be defined.
79+
When the code associated with a symbol is defined at a different time than this access, if the `-Yretain-trees` compilation option is not used, then the `tree` of the symbol will not be available.
80+
Symbols originating from Java code do not have an associated `tree`.
8381

8482
### Obtaining a `TypeRepr` from a `Symbol`
8583

86-
In the previous paragraph we saw that `Symbol.tree` should be avoided and
87-
therefore you should not use `sym.tree.tpe` on `sym: Symbol`. Thus to obtain
88-
the `TypeRepr` corresponding to a `Symbol`, it is recommended to use
89-
`tpe.memberType` on objects `tpe: TypeRepr`.
84+
In the previous heading, we saw that `Symbol.tree` should be avoided and that therefore you should not use `sym.tree.tpe` on `sym: Symbol`.
85+
Thus, to obtain the `TypeRepr` corresponding to a `Symbol`, it is recommended to use `tpe.memberType` on `tpe: TypeRepr` objects.
9086

9187
We can obtain the `TypeRepr` of `Leaf` in two ways:
9288
1. `TypeRepr.of[Box.Leaf]`
93-
2. `boxTpe.memberType(leafSym)`, in other words we request the `TypeRepr` of
94-
the member of `Box` whose symbol is equal to the symbol of sym
89+
2. `boxTpe.memberType(leafSym)`
90+
(In other words, we request the `TypeRepr` of the member of `Box` whose symbol is equal to the symbol of `leafSym`.)
9591

96-
while the two approaches are equivalent, the first is possible only if you
97-
already know that you are looking for `Box.Leaf`. The second approach allows
98-
you to explore an uknown API.
92+
While the two approaches are equivalent, the first is only possible if you already know that you are looking for the type `Box.Leaf`.
93+
The second approach allows you to explore an unknown API.
9994

10095
### Use `Symbol`s to compare definitions
10196

10297
Read more about Symbols [here][symbol].
10398

104-
Symbols allow comparing definitions using `==`:
99+
Symbols allow you to compare definitions using `==`:
105100
```scala
106101
leafSym == baseSym.children.head // Is true
107102
```
@@ -113,7 +108,7 @@ boxTpe.memberType(baseSym.children.head) == leafTpe // Is false
113108

114109
### Obtaining a Symbol for a type
115110

116-
There is a handy shortcut to get the symbol of the definition of `T`.
111+
There is a handy shortcut to get the symbol for the definition of `T`.
117112
Instead of
118113

119114
```scala

_overviews/scala3-macros/tutorial/macros.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ As a technicaly consequence, we cannot define and use a macro in the **same clas
7373
However, it is possible to have the macro definition and its call in the **same project** as long as the implementation of the macro can be compiled first.
7474

7575
> ##### Suspended Files
76-
> To allow defining and using macros in the same project, only those calls to macros are expanded, where the macro has already been compiled.
76+
> To allow defining and using macros in the same project, only those calls to macros that have already been compiled are expanded.
7777
> For all other (unknown) macro calls, the compilation of the file is _suspended_.
7878
> Suspended files are only compiled after all non suspended files have been successfully compiled.
7979
> In some cases, you will have _cyclic dependencies_ that will block the completion of the compilation.
@@ -206,7 +206,7 @@ def sumCode(nums: Expr[Seq[Int]])(using Quotes): Expr[Int] =
206206
The extractor will match a call to `sumNow(1, 2, 3)` and extract a `Seq[Expr[Int]]` containing the code of each parameter.
207207
But, if we try to match the argument of the call `sumNow(nums: _*)`, the extractor will not match.
208208

209-
`Varargs` can also be used as a constructor, `Varargs(Expr(1), Expr(2), Expr(3))` will return a `Expr[Seq[Int]]`.
209+
`Varargs` can also be used as a constructor. `Varargs(Expr(1), Expr(2), Expr(3))` will return an `Expr[Seq[Int]]`.
210210
We will see how this can be useful later.
211211

212212

@@ -226,8 +226,8 @@ while subsequent chapters introduce the more advanced APIs.
226226
### Collections
227227

228228
We have seen how to convert a `List[Int]` into an `Expr[List[Int]]` using `Expr.apply`.
229-
How about converting a `List[Expr[Int]]` into `Expr[List[Int]]`?
230-
We mentioned that `Varargs.apply` can do this for sequences -- likewise for other collection types, corresponding methods are available:
229+
How about converting a `List[Expr[Int]]` into an `Expr[List[Int]]`?
230+
We mentioned that `Varargs.apply` can do this for sequences; likewise, for other collection types, corresponding methods are available:
231231

232232
* `Expr.ofList`: Transform a `List[Expr[T]]` into `Expr[List[T]]`
233233
* `Expr.ofSeq`: Transform a `Seq[Expr[T]]` into `Expr[Seq[T]]` (just like `Varargs`)
@@ -269,7 +269,7 @@ Note, that `matches` only performs a limited amount of normalization and while f
269269
### Arbitrary Expressions
270270

271271
Last but not least, it is possible to create an `Expr[T]` from arbitary Scala code by enclosing it in [quotes][quotes].
272-
For example `'{ ${expr}; true }` will generate an `Expr[Int]` equivalent to `Expr.block(List(expr), Expr(true))`.
272+
For example, `'{ ${expr}; true }` will generate an `Expr[Int]` equivalent to `Expr.block(List(expr), Expr(true))`.
273273
The subsequent section on [Quoted Code][quotes] presents quotes in more detail.
274274

275275
[contributing]: {% link scala3/contribute-to-docs.md %}

0 commit comments

Comments
 (0)