Code-Walking Macros

Let’s say you’re a former Ruby developer, and you really miss the feature of Ruby that allows any method, class, or module definition to act as an implicit begin (Ruby’s version of try). In other words, you’d like to be able to write this code in Clojure:

language_features/implicit_try_1.clj
 
(​defn​ delete-file [path]
 
(clojure.java.io/delete-file path)
 
(​catch​ java.io.IOException e false))

This code doesn’t work in Clojure, of course—you’ll get a compiler exception if you try to type that in at your REPL. You’d need to wrap a try expression around everything following the argument list to get it to work as you want. Take a moment and think about what it would take to have this code work. One option would be to file an ...

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.