Home

macroexpansion

Macroexpansion is the process by which macros are expanded into ordinary program code. It typically occurs at compile time or interpretation time, before runtime, enabling code generation from compact templates and enabling domain-specific abstractions.

A macro is a construct that takes code as input and returns new code. The compiler or

Two broad approaches exist: textual macros and hygienic macros. Textual macros substitute text and do not respect

Examples and varieties. In Lisp-family languages, macros operate on code as data structures and can generate

Impact and considerations. Macro expansion enables metaprogramming, embedding domain-specific languages, and moving computations to compile time

See also macro, macro hygiene, Lisp, Rust, C preprocessor.

preprocessor
runs
the
macro
definitions
to
produce
an
expanded
program,
which
is
then
compiled
or
executed.
In
languages
with
macro
systems,
there
is
often
a
mechanism
such
as
macroexpand
or
macroexpand-1
to
display
or
control
the
expansion
step
by
step.
lexical
scope,
which
can
lead
to
name
collisions
and
subtle
bugs.
Hygienic
macros
preserve
lexical
scoping
by
renaming
introduced
identifiers
and
controlling
variable
capture,
reducing
these
risks.
arbitrary
code,
with
constructs
like
defmacro
and
macroexpand
illustrating
the
approach.
In
C,
the
preprocessor
implements
textual
macros
via
#define,
performing
simple
text
substitution
and
lacking
hygiene
or
type
awareness.
Modern
languages
such
as
Rust
provide
both
declarative
macros
(macro_rules!)
and
procedural
macros,
with
hygiene
features
to
avoid
unintended
interactions
with
surrounding
code.
to
improve
efficiency.
However,
macro-related
errors
can
be
hard
to
diagnose,
since
the
programmer
often
interacts
with
the
expanded
code
rather
than
the
macro
source.