Skip to content
uencoder

Macro

A macro expands code at compile time, enabling metaprogramming: code that generates code, avoiding duplication and boilerplate.

Purpose

Compile-time code transformation, eliminating boilerplate and implementing DSLs.

Mechanism

C macros: textual substitution (preprocessor). Rust macros: hygienic macros expanding to AST. Lisp macros: code as data. JS tagged template literals. Macros are more flexible than functions but harder to debug — use with care.

Use cases

  • Code generation
  • DSL construction
  • Boilerplate elimination

Related terms

References