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

Macroexpansion

Manually walking through a macroexpansion, as you saw in the previous section, is fairly tedious work. But as you might guess, you don’t always have to do it manually. Clojure exposes to users the very same macroexpansion tools that it uses internally. This way, you can have a look at the expression a macro will generate, without having to go through that whole process you just saw.

macroexpand-1 is the simplest of these tools, converting a macro expression to its resulting expression.

basics/macroexpand_1.clj
 
(​macroexpand-1​ '(​when​ (​=​ 1 2) (​println​ ​"math is broken"​)))
 
;=> (if (= 1 2) (do (println "math is broken")))
 
 
(​macroexpand-1​ nil)
 
;=> nil
 
 
(​macroexpand-1​ '(​+​ 1 2))
 
;=> (+ 1 2)

Note the similarity ...

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