August 2014
Intermediate to advanced
120 pages
2h 45m
English
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>}} ... |
Read now
Unlock full access