Hygienic macros (was: do...until wisdom needed...)

Douglas Alan nessus at mit.edu
Tue Apr 17 20:59:52 EDT 2001


"Brett g Porter" <BgPorter at NOartlogicSPAM.com> writes:

> Hi -- for the non-LISP-ers among us -- could someone give a précis
> on what makes a macro 'hygienic'? A quick Google search turned up
> tons of links, but none that were explanatory (at least not to a
> non-LISP programmer like myself), and ESR's Jargon File doesn't
> cover it.

Lisp has traditionally had "procedural macros".  Procedural macros in
Lisp are a kind function -- they are implemented in Lisp, but the
return value of the macro, rather than being returned as a function
value, is taken to be a piece of code.  This is easier in Lisp than
in most languages because Lisp code is a kind of Lisp data structure.
The code that is returned by the macro call, which is executed at
compile-time, is used to replace the original call to the macro.

This allows a programmer to define new syntactic forms. These behave
differently from normal function calls, since they can do things like
rebind variables in the scope of the macro invocation, etc., and they
avoid procedure call overhead, so they can be used to effectively
force inlining.

The problem with non-hygienic macros is that variable names used by the
macro implementation can conflict with variable names that are passed
into the macro.  Hygienic macros solve this problem by putting the
variables in different namespaces.

Hope this helps.

|>oug



More information about the Python-list mailing list