Hacker News new | ask | show | jobs
by Rovanion 1645 days ago
I think the lack of reader macros really help here. Macros in Clojure are much more "functions that run at compile time" rather than "look at this fun DSL I threw together".

What I think it comes down to though is adherence to convention. A preference hierarchy has grown together with Clojure and it goes:

1. Can it be expressed using data structures, do that. For example a map/dictionary lookup. 2. If that fails, express your more complex idea using a function. 3. If you absolutely must, write a macro.

You usually end up with very few macros except for those in the standard library, and those you end up with are more commonly of the convenience kind. One example would be the standard web routing library of the early 10's called Compojure that relied heavily on macros where my current favourite Reitit uses just data.

1 comments

The last time “macros in Clojure” came up, I searched through some of the libraries I’ve run across to see how often they’re used and, as you suggest, it ain’t much.

Re-frame has just one, and it looks like it’s used for testing. Clara-rules has eight that are used in the library, which implements a forward-chaining Rete rule engine. Core.logic has a handful, and that’s essentially “prolog inside Clojure.” So clearly those libraries get a lot of mileage out of data and functions, with just a dash of macro.

I have yet to reach for them myself, and I can’t imagine a situation where I’d need a macro instead of a function. I think they’re probably great for library developers who are smarter than me, and I’m happy to leave them to it.

I think they come in handy when you've got some functionality that has to happen in a context that you can't easily move between function calls... Then you benefit from the ability to template into the description of that context some actions to perform _before_ you enter into the "black box".

That's pretty abstract but honestly its not really such an earthshattering feature. I'd argue it's pretty much necessary for any "final" programming language to be infinitely metaprogrammable (i.e. a lisp-3) but we don't have a mature language like that yet. Probably it will arrive from laskell/hisp kinda synthesis.