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

Macros Can Be Contagious

Next we’ll look at an important consequence of macros not being values: macros that take a variable number of arguments can infect their callers, forcing the author to write more macros instead of functions. Let’s take a look at an example and think about the implications for calling code:

beware/contagious_1.clj
 
(​require​ '[clojure.string :as string])
 
(​defmacro​ log [& args]
 
`(​println​ (​str​ ​"[INFO] "​ (string/join ​" : "​ ~(​vec​ args)))))
 
 
user=> (log ​"that went well"​)
 
;[INFO] that went well
 
;=> nil
 
user=> (log ​"item #1 created"​ ​"by user #42"​)
 
; [INFO] item #1 created : by user #42
 
;=> nil

If you recall what you learned in Chapter 2, Advance Your Macro Techniques, you’ll notice that we’re ...

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