John Fremlin's blog: Reflecting on meta-programming

Posted 2011-02-20 14:11:00 GMT

I've been having revealing conversations with people in the Scala and F# communities where I discover that my definition of meta-programming is not widely shared. Personally, I think that the two most misunderstood computer languages are C++ and Lisp: and one reason for that is that they both offer real meta-programming — in the sense not of Ruby's dynamic reflection and redefinition, but in the sense that one can write programs to write other programs.

Reflection, in terms of looking at what functions and types have already been defined, is in my view normal programming — however, fiddling around dynamically at run-time depending on the compile-time definitions of the program, while probably ugly and inefficient, is able to work around problems that would naturally be solved with meta-programming facilities, if they were available. This does not excuse their absence, just makes it slightly less unbearable and introduces unnecessary runtime costs.

Can one come up with a well-defined real-world example task that can be solved with meta-programming but not reflection? Of course not — meta-programming does not make a system more than Turing complete. In the same way that anything achievable with subroutines can be achieved with gotos. The argument that must be advanced for meta-programming is more nuanced: for someone who has never imagined a function call, then a tangled spaghetti of goto statements is all they see before them.

Writing programs to write programs is a powerful tool which naturally side-steps whole classes of systemic deficiencies; being able to do it fluidly in Lisp rather than in a contrived and round-about fashion with C++ template trickery renders it appropriate for many more tasks. I hope to present a few strong examples from the tpd2 project in greater detail - more are welcome!

Post a comment