Monday, July 31, 2006

First Thoughts on Erlang Metaprogramming

During my acquintance with Erlang, I've heard a number of people complain about Erlang's limited metaprogramming abilities. The only "official" metaprogramming tool Erlang provides is the preprocessor, whose logic is limited to ifdef..else statements, so these complaints seem quite justified.

However, in my recent digging through Jungerl code, I came across the rdbms_codegen module, written by Ulf Wiger, and it gave me a different perspective on the issue. In fact, this code has made me wonder whether Erlang's metaprogramming capabilities are actually unmatched by any other language. Why? Because this code shows that not only can you programmatically generate and parse abstract Erlang syntax trees, but when mixing it with Erlang hot code swapping, you can also hot-deploy this code in runtime, thereby creating dynamically mutating applications! How cool is that?

I'm not surprised that many people aren't aware of this capability because, like many things Erlang, it's not very well documented :) To use it, you need to understand the Erlang abstract format and to be familiar with the erl_parse and the code module. You also need to be able to piece everything together, which is far from obvious. Thankfully, we can all benefit from Ulf Wiger's work, which gives us a good example of how to accomplish this wizardry.

I'll probably write more about Erlang metaprogramming in the future, after I've delved deeper into this arcane and fascinating land.


murphee said...

Lack of metaprogramming was a bit of downer when I heard about it, but it seems like tha twas a misconception.
I'd be very interested in anything you can dredge up about this.

Yariv said...

There are some very interesting applications that I have in mind for this. Stay tuned.