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 ...
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.