O'Reilly logo

Mastering Clojure Macros by Colin Jones

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Secret Macro Voodoo

In addition to all of the syntax-quoting tools that Clojure provides for both macros and normal programming, we have two special values, &form and &env, that are available only within macros. Both of these allow us to introspect a bit on the way a macro gets used. Let’s see what information we have available when we use them:

advanced_mechanics/secret_macro_variables_1.clj
 
(​defmacro​ info-about-caller []
 
(pprint {:form &form :env &env})
 
`(​println​ ​"macro was called!"​))
 
 
user=> (info-about-caller)
 
;{:form (info-about-caller), :env nil}
 
;macro was called!
 
;=> nil
 
user=> (​let​ [foo ​"bar"​] (info-about-caller))
 
;{:form (info-about-caller),
 
; :env {foo #<LocalBinding clojure.lang.Compiler$LocalBinding@23ef55fb>}} ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required