hawkeye
hawkeye

Reputation: 35692

Anonymous macros in Clojure

In Common LISP you can do the following

(macro lambda (x) (list (quote car) (list (quote cdr) x)))

It looks like this is not possible (an anonymous macro) in Clojure.

  1. Is this true?
  2. Why was this left out?

Upvotes: 4

Views: 1170

Answers (2)

mikera
mikera

Reputation: 106351

It is my understanding (from reading the documentation and the source) that Clojure currently (as of version 1.2) only supports named macros defined with defmacro.

It's possible however that you can abuse the internals of Clojure to get some sort of anonymous macro by directly calling the Java function Var.setMacro() on some anonymous var. This is what defmacro uses under the hood to construct macros.

e.g. you can do:

(def macro-var (var (fn [...] ...)))

(.setMacro macro-var)

And the Clojure reader will subsequently interpret the var contained in macro-var as a macro. However you'll have to figure out how to define everything correctly and it might all break in future versions of Clojure.

Clojure 1.3 (currently in alpha, but fully operational) brings various other enhancements, e.g. symbol macros, so might also be worth a look.

Upvotes: 1

kotarak
kotarak

Reputation: 17299

There is some macrolet (or other things) in clojure.contrib.macro-utils. However it smells a little like a hack to me. Inclusion in clojure.core is on the TODO radar, but doesn't have high priority at the moment.

EDIT: See also here http://dev.clojure.org/display/design/letmacro.

Upvotes: 4

Related Questions