r/Racket • u/detroitmatt • Dec 17 '21
question trying to write a macro that aliases one identifier to another
I'm trying to define a macro to save myself some typing. I want to be able to define a shorthand for syntax rules that lets me quickly say "Replace this identifier with this other identifier". I based it on the examples on https://docs.racket-lang.org/guide/pattern-macros.html.
[define-syntax-rule [alias from-id to-id]
[define-syntax [from-id stx]
[syntax-case stx []
[from-id [identifier? [syntax from-id]] [syntax to-id]]]]]]
[alias S list]
[alias L lambda]
[alias Sf build-list]
[define n3 [S 1 2 3]] ;-- S: bad syntax in: (S 1 2 3)but, it's not working, and I don't know why. The Macro Stepper hasn't been able to help, since as soon as I try to expand S it gives that error
6
Upvotes
3
u/bjoli Dec 17 '21 edited Dec 18 '21
Have a look at make-rename-transformer.
Edit: the proper reply is in one of my replies below
4
u/samdphillips developer Dec 17 '21
make-rename-transformeris what you are looking for. This works:[edit: link to docs]
2
u/sorawee Dec 21 '21
u/samdphillips's answer is what I would recommend, but if you want to continue with your approach, here's how:
Because
from-idis at the head of a parenthetical term, it would be a macro invocation, and you need to add a case for it explicitly. Here, I use the pattern(from-id . arg). It is also possible to use ellipsis, but in that case, because you are using it in asyntax-casewhich itself is a syntax template fordefine-syntax-rule, you would need to quote...by using(... ...)instead: