Symbol macros and local macros

109 views
Skip to first unread message

Konrad Hinsen

unread,
May 5, 2009, 9:54:49 AM5/5/09
to Clojure
I just added a new library to clojure.contrib: clojure.contrib.macro-
utils. It provides three macros that are useful in writing other macros:

- macrolet (syntax like letfn) provides local macros that are active
only inside the body of the macrolet form.

- symbol-macrolet provides local symbol macros.

- defsymbolmacro defines global symbol macros.

Symbol macros are textual replacements for symbols; they have no
parameters. Wherever a symbol defined as a macro occurs in a form, it
is replaced by the symbol macro's value, EXCEPT where the symbol
names a function argument or receives a binding in a let form. The
replacement value is again subjected to macro expansion, until the
result no longer changes. This is the same behaviour as for standard
macros.

While macrolet and symbol-macrolet just work as expected, global
symbol macros require the use of the (with-symbol-macros ...) form
around the expression(s) where they are used. This is because
Clojure's macro system doesn't know anything about symbol macros
otherwise. (with-symbol-macros ...) uses a complete re-implementation
of macro expansion that knows about symbol and local macros. The same
implementation is used by macrolet and symbol-macrolet. The
expressions inside these forms are handed back to Clojure's compiler
already fully expanded.

Macrolet and symbol-macrolet are most useful in defining other
macros. Typically, a macro would expand into a form containing
macrolet and/or symbol-macrolet for defining values or operations for
use inside the body of the expansion.

The library also provides three functions for testing and debugging
macro expansions:

- mexpand-1 works like macroexpand-1 but knows about symbol macros
- mexpand works like macroexpand but knows about symbol macros
- mexpand-all does a full recursive macro expansion

Any feedback is welcome.

Konrad.

Tom Faulhaber

unread,
May 11, 2009, 2:33:47 PM5/11/09
to Clojure
Konrad,

Your post didn't get any feedback, but I wanted to say that this is a
very nice piece of functionality and will be a great help when
creating DSLs in Clojure.

I look forward to taking advantage of it.

Thanks,

Tom
Konrad,

Your post didn't get any feedback, but I wanted to say that this is a
very nice piece of functionality and will be a great help when
creating DSLs in Clojure.

I look forward to taking advantage of it.

Thanks,

Tom
Reply all
Reply to author
Forward
0 new messages