Hacker News new | ask | show | jobs
by DennisP 5933 days ago
Use Clojure to generate your Java code. As far as anyone else is concerned you're writing Java.

I've used code generation at work quite a bit, whenever I've had somewhat repetitive code to write that I couldn't factor away. It makes my job a lot less tedious and more fun.

1 comments

Is there any public Clojure code for this?
I have no idea. It's easy enough to make special-purpose code generators yourself...don't bother trying to make some kind of universal system, just hack out code to generate exactly what you need for your current project. You may see ways to generalize it later.

I'm at a C# shop myself, and generally just write my generators in C#. Just spitting out strings of sourcecode and writing them into files. I would think Clojure would be better for this sort of thing, since it's got macros.

On the other hand, doing it in C# worked really well for one project, because after writing the generator I realized that I could convert it from a compiler into an interpreter, and make a program that didn't need a generator after all because it was so flexible at runtime.

There's quite a few examples of code generators (e.g. for GUI's, database access, write test fixtures/setup/teardown, etc) in ocaml. It is one of Harrop's success stories. The Manning Code Generation book by Harrington is from 2003, uses ruby 1.6, but i remember it being an excellent book

http://www.amazon.com/Code-Generation-Action-Jack-Herrington...