10 ms·
Clojure Macros and Metaprogramming
- adamddev1 5y agoDoes anyone know how these compare to macros in Racket?
- disconcision 5y agoclojure macros are quite retro and minimalistic relative to racket's. this is partly an intentional decision; clojure and racket are basically opposite extremes of lisps when it comes to how big a role macro authorship is supposed to play in the ecosystem. personally i'd say that while clojure's decision to de-emphasize macros is, given the rest of the language design, a good one, it's slightly unfortunate that clojure macros don't incorporate some of safety and ergonomic features from scheme/racket, e.g. hygiene by-default and more able declarative templates aka racket/syntax-parse.
- roenxi 5y agoFAQ: But what are macros for and why not functions? A: As you can see in the linked article, they do lots of things but macros are really useful for creating new control structures. Things like threading macros (->), (and), (for), (time), etc. It is difficult to create new control structures in languages that don't have good macros. Try implementing new flow control using functions and appreciate how hard it is. This is why a lisp, like Clojure, can add new control structures using a library. Many languages struggle to do that and rely on some committee somewhere deciding it is a good idea.
- User23 5y agoTo add to this, if you grab SBCL or another free CL and dig around you’ll see a ton of the language is implemented as macros. I haven’t looked at the Clojure source but I imagine it’s similar.
- srcreigh 5y agoImagine adding an entire type system to the language as a library. That's what typed racket did (in 2014-ish?) with racket using macros.
- Ambroisie 5y agoI'm surprised the `gensym` machinery is so "naive" as in it could actually be possible to craft a symbol that can refer to a `gensym`ed symbol. I am not very familiar with other languages that implement (un)hygienic macros, is that a common problem in things like Scheme, Lisp, or even Rust, or do they all circumvent that issue with actually private/un-nameable symbols?
- thom 5y agoThe sum total of people who have or ever will come to grief because of this design is zero, surely?
- gleenn 5y agoRight, avoid using symbols that look like gensym stuff. It would be pretty silly to do something like that. If I saw someone using "G__1234" then that would be a pretty big eyebrow raiser.
- fmakunbound 5y agoI wish Clojure’s reader wasn’t so needlessly limited.
- CraigJPerry 5y agoHow do you use lisp style macros when you have 500 developers working on a single product? If you have 500 developers on a java project, you tell the new person which version of java you’re using, you mention whatever libraries or frameworks you lean on heavily and you give them a walk through the subsystems, their key components and essential abstractions and then you spend time showing how the build system is setup and how to create an environment for the system. Then they start knocking out features. There’s a bit more than I’ve said but overall It’s pretty straightforward, any java-for-hire person knows the score and everyone just cracks on with it. How do you do this when the language itself is programmable? Clojure probably avoids my concern by two facts - it embraces immutability and it only has macros, it doesnt have reader macros. Common Lisp has neither of those - it might be common to write in an immutable style but in Common Lisp you can (setf ) til your heart’s content. You can also define reader macros - i recently followed an example to implement json parsing in the reader. I’m still equal parts impressed, amazed and horrified. So. You can probably tell I’ve fallen for lisp in a big way, but how do you scale up to a big dev team? Much as i love lisp in my short journey so far, it seems like a super power that could only be leveraged by up to a “two pizza team”.
- medo-bear 5y ago> how do you scale up to a big dev team? can you explain why this is such a big concern? as far as i can tell, you can set development guidelines and employ people who will stick to the rules. it's not like you can sneak in macros and present them as functions
- CraigJPerry 5y agoWhat happens when you scale java development to a team of that size? Short version: every optimisation you can dream of gets implemented. Clever use of Off heap datastructures aren’t going to be enough to impress. Forget thinking your custom class loader with cooperating javaAgent that manipulates the bytes of classes to dynamically add or remove instrumentation is going to impress. These things are just taken for granted. Even a bot that identifies dead code after traversing the AST, then re-generates valid source code from the (now pruned) AST, runs the test suite then checks in the change all by itself is seen as meh after a while. No matter how out-there an idea is, the play area is constrained by some immutable platform rules (the java memory model). So no matter how funky things get, any other java dev will still be able to reason about any code in the system. What happens when you remove those platform constraints? What happens when you say, we started off with lisp but we’ve ended up with something quite bespoke?
- d_t_w 5y agoI've been programming in Clojure professionally for roughly ten years now. It's a great language and extremely effective if you're building products both on the JVM and in the browser. I think I've written a handful of macros in that time, there is one in my current codebase in a test class, and it could probably be removed. Macros are for extending the language. Programmers who are new to Clojure tend to pick macros up and get a bit excited and create lots of DSLs and other cute little trinkets, and then they put macros down again because they realise they don't compose. The power of Clojure is in simple datastructures, the core functions that operate on them, composition, and host interop. The most useful thing that I made with macros was a library for opinionated exception handling for Clojure core.async, at the end of which I realised I was using core.async a bit too much and ended up removing a bunch of it. The product I work on today is an Enterprise toolkit for Apache Kafka (https://kpow.io https://kpow.io) that according to Github is 97% Clojure. Data-oriented, great delivery cadence, and terrific fun to work on. Having a single language on the JVM and in the Browser is enormous leverage, certainly we feel the benefit every day.
- eyelidlessness 5y agoIt’s been several years since I’ve worked in Clojure. When I started to dive into the language, I was warned not to write macros without a good justification (and heeded those warnings). One of my most memorable projects[1] relied heavily on macros. I knew at the time that I was using the tools I had to automate a performance optimization technique. What I didn’t know at the time was that my former (and once again future) ecosystem, JavaScript (now TypeScript), was going through a tooling revolution (of sorts, no value attached to that observation). Babel, and then countless other tools, were transforming more and more JS. Now that sort of thing is pretty much the default. All of that is to say: macros are not a silver bullet, they’re not trivial to write/read/maintain… but goodness dealing with similar use cases in JS environments routinely makes me long for them. The amount of esoteric knowledge required to build something similar to my 150 line library in a JS context is absolutely bonkers. Versus just using built in language features and calling runtime functionality as needed at build time. I don’t want to overly romanticize either macros generally or Clojure specifically. But I sincerely believe a large part of the frustration with the JS ecosystem from developers’ perspective would be significantly less frustrating if manipulating the language and sharing build/runtime logic were part of the language itself. 1: https://github.com/reup-distribution/espalier https://github.com/reup-distribution/espalier