Skip to content

Commit 843b9f8

Browse files
committed
Add section describing why new syntax is needed
1 parent fb506f2 commit 843b9f8

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

text/0000-macro-metavar-expr.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,49 @@ expressions. Available alternatives are:
311311
* Another sigil, although `#` should be avoided to avoid clashes with the
312312
`quote!` macro.
313313

314+
## Why not a proc macro or built-in macro?
315+
316+
To avoid extending the language with new syntax, we could consider writing
317+
something that looks like a macro invocation, such as `count!(value)`, which
318+
would be implemented as a procedural macro or built-in to the compiler.
319+
320+
While this is compelling from a language simplicity perspective, it creates
321+
some problems due to the way macro expansions are processed. During macro
322+
transcription, other macro invocations are not evaluated, so in the macro:
323+
324+
```
325+
macro_rules! example {
326+
($($x:ident),*) => count!(x)
327+
}
328+
```
329+
330+
During transcription, `example!(a, b, c)` would expand to `count!(x)`. At this
331+
point, the knowledge of the metavariable `x` and its repetition is lost, and
332+
no procedural macro or built-in macro would be able to work out the count.
333+
334+
To workaround this we would need to re-expand the repetition
335+
(`count!($($x),*)`, forcing the `count!` macro to re-parse and count the
336+
repetitions. This is additional unnecessary work that this RFC seeks to
337+
address.
338+
339+
Another way to think of metavariable expressions is as "macro transcriber
340+
directives". You can think of the macro transcriber as performing the
341+
following operations:
342+
343+
* `$var` => the value of `var`
344+
* `$( ... ) ...` => a repetition
345+
346+
This RFC adds two more:
347+
348+
* `${ directive(args) }` => a special transcriber directive
349+
* `$$` => `$`
350+
351+
We could special-case certain macro invocations like `count!` during
352+
transcription, but that feels like a worse solution. It would make it harder
353+
to understand what the macro transcriber is going to do with arbitrary code
354+
without remembering all of the special macros that don't work like other
355+
macros.
356+
314357
# Prior art
315358
[prior-art]: #prior-art
316359

0 commit comments

Comments
 (0)