Today, instead of `syntax-rules`, you'll probably use `syntax-case`, which is a lot easier to use for harder purposes, and also easier error-checking, just not as interesting as `syntax-rules`. Or, if you're using Racket, `syntax-parse`.
Greg Hendershott's "Fear of Macros" should be an easy introduction to some of the newer ways, though it has some Racket-specific bits in it: https://www.greghendershott.com/fear-of-macros/
I still use `syntax-rules` a lot for simple macros - there's hardly any boilerplate and macros come out very readable without all the quasi-quoting, quasi-unquoting etc. that happens in other macro systems.
But some things just can't be accomplished with it. I only turn to `syntax-case` for these things, like introducing new identifiers, or fully procedural tasks. It blew my mind when I discovered that I could write a macro that actually reads files from disk and binds variables to each path at macro-expansion time (not saying that's a great idea though...)
I like `syntax-rules` and have done some crazy things with it. If I were going to teach someone syntax extension in Scheme or Racket, however, I'd just use `syntax-case` or `syntax-parse` for everything. That avoids confusing people with two different languages.
Making define-syntax bind to a name that appears in the body would be confusing. You can omit the macro name in the pattern and use an underscore instead.
10 comments
[ 3.0 ms ] story [ 30.1 ms ] threadToday, instead of `syntax-rules`, you'll probably use `syntax-case`, which is a lot easier to use for harder purposes, and also easier error-checking, just not as interesting as `syntax-rules`. Or, if you're using Racket, `syntax-parse`.
Greg Hendershott's "Fear of Macros" should be an easy introduction to some of the newer ways, though it has some Racket-specific bits in it: https://www.greghendershott.com/fear-of-macros/
But some things just can't be accomplished with it. I only turn to `syntax-case` for these things, like introducing new identifiers, or fully procedural tasks. It blew my mind when I discovered that I could write a macro that actually reads files from disk and binds variables to each path at macro-expansion time (not saying that's a great idea though...)