How Now, Clojure Macros?

With Clojure, I now need to take the attitude I had towards Python two and 1/2 years ago. I knew that our automated water meter reading project’s beginning was looming, and I would have to start designing and implementing code in Python, so I chose small tasks to get used to the language. Therefore, I know to use Clojure for something, even if it is script-like and small. That is the advice Amit Rathore gave his recent Clojure course, and what I have read in Clojure books and in Clojure blogs and articles. So, OK, that’s not hard to understand or to carry out.

But what is troubling me are how to see where and why to implement Clojure macros. I am a believer in using language features. Of course you can abuse anything. In Bliss, the macro system was so powerful that some DEC engineers rewrote Bliss so it looked like Basic. Some early C++ implementors (mid 1990s) went of screaming into the weeds, and had all kinds of unmanageable objects using single and multiple inheritance without thinking first. It did not mean class inheritance was a bad thing.

The big question for me now is why and where do I want to use Clojure macros, and how will my mind figure this out instinctively. I have read quite a few good Clojure books on the subject and studied the examples. I like the idea of having the macros do a lot of work at compile, instead of, run time. I’m not sitting around waiting for it to happen by divine miracle, so onward with drumming up some Clojure work, and trying to see one day what others seem to easily today.





1 Comment

Filed under Clojure

One response to “How Now, Clojure Macros?

  1. Alex Baranosky

    Generally, you use macros when you can’t use a function (there are a small handful of exceptions). Next time you see some duplication that you want to create a function for… but CAN’T, that is the time to choose a macro. An example might be if you wanted to create a new kind of cond, or a enhanced version of def, perhaps you want some code to generate multiple multimethods at once or you want to generate multiple variants of the same function definition.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s