Macro systems vary widely in safety and power across programming languages. C macros operate on token streams with minimal safety, while Lisp macros work on ASTs enabling more powerful transformations. Scheme introduced hygiene to prevent variable capture, and Racket further improved safety with phase separation and scope sets. Modern languages like Elixir implement robust hygienic macros, while Rust offers both pattern-based and procedural macros. The best macro systems operate on ASTs, maintain hygiene, produce clear error messages, and enforce phase separation. While functions should be preferred when possible, well-designed macros enable library authors to extend languages with new syntactic constructs without modifying the core language.

13m read timeFrom lambdaland.org
Post cover image
Table of contents
C macros: advanced search-and-replace #Lisp macros: operating on ASTs #Scheme macros: hygiene #Racket macros: phase separation and scope sets #Other languages #Conclusion #References #
265 Impressions