Macros Can Be Tricky to Get Right

You’ve already seen plenty of subtleties with macros, such as the need to avoid symbol capture with gensyms, and there’s more to come. While you’re in the great position of knowing all the language’s rules for macro construction, you may still be unclear about the implications of what you know. Let’s look at a few more ways you might accidentally cause yourself grief by making the wrong assumptions about macro code.

Here’s a re-implementation of the clojure.core/and macro that seems fairly straightforward.

beware/and_1.clj
 
(​defmacro​ our-and
 
([] true)
 
([x] x)
 
([x & ​next​]
 
`(​if​ ~x (our-and ~@​next​) ~x)))
 
 
user=> (our-and true true)
 
;=> true
 
user=> (our-and true false)
 
;=> false
 
user=> (our-and ...

Get Mastering Clojure Macros now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.