Skip to content

Commit 8bfe721

Browse files
Merge pull request #12110 from blast-hardcheese/patch-1
macros.md: Documentation does not match example
2 parents d421955 + 3e64877 commit 8bfe721

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

docs/docs/reference/metaprogramming/macros.md

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,22 +186,24 @@ Expr.betaReduce(_): Expr[(T1, ..., Tn) => R] => ((Expr[T1], ..., Expr[Tn]) => Ex
186186
Types are not directly affected by the phase consistency principle.
187187
It is possible to use types defined at any level in any other level.
188188
But, if a type is used in a subsequent stage it will need to be lifted to a `Type`.
189-
The resulting value of `Type` will be subject to PCP.
190189
Indeed, the definition of `to` above uses `T` in the next stage, there is a
191190
quote but no splice between the parameter binding of `T` and its
192-
usage. But the code can be rewritten by adding a binding of a `Type[T]` tag:
191+
usage. But the code can be rewritten by adding an explicit binding of a `Type[T]`:
193192

194193
```scala
195-
def to[T, R](f: Expr[T] => Expr[R])(using Type[T], Type[R], Quotes): Expr[T => R] =
196-
'{ (x: T) => ${ f('x) } }
194+
def to[T, R](f: Expr[T] => Expr[R])(using t: Type[T])(using Type[R], Quotes): Expr[T => R] =
195+
'{ (x: t.Underlying) => ${ f('x) } }
197196
```
198197

199198
In this version of `to`, the type of `x` is now the result of
200-
splicing the `Type` value `t`. This operation _is_ splice correct -- there
201-
is one quote and one splice between the use of `t` and its definition.
199+
inserting the type `Type[T]` and selecting its `Underlying`.
200+
201+
To avoid clutter, the compiler converts any type reference to
202+
a type `T` in subsequent phases to `summon[Type[T]].Underlying`.
203+
204+
And to avoid duplication it does it once per type, and creates
205+
an alias for that type at the start of the quote.
202206

203-
To avoid clutter, the Scala implementation tries to convert any type
204-
reference to a type `T` in subsequent phases to a type-splice, by rewriting `T` to `summon[Type[T]].Underlying`.
205207
For instance, the user-level definition of `to`:
206208

207209
```scala
@@ -213,7 +215,10 @@ would be rewritten to
213215

214216
```scala
215217
def to[T, R](f: Expr[T] => Expr[R])(using t: Type[T], r: Type[R])(using Quotes): Expr[T => R] =
216-
'{ (x: t.Underlying) => ${ f('x) } }
218+
'{
219+
type T = t.Underlying
220+
(x: T) => ${ f('x) }
221+
}
217222
```
218223

219224
The `summon` query succeeds because there is a given instance of

0 commit comments

Comments
 (0)