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>}} ... |
Get Mastering Clojure Macros now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.