10

How often do you or have you seen meta-programming be used in projects? In uni I've never seen this be applied, but I've seen this on my previous job (when I saw it I was blown away of how effective it was).

But how common is it? Is it used all the time, or just occasionally?

gablin
  • 17,377
  • 22
  • 89
  • 138
  • 5
    It might not be that common; though quite common in Lisp, Lisp is not that common -- not even Common Lisp. – paul Oct 10 '12 at 16:03
  • explicit or implicit? At one extreme using Java and C# is arguably meta-programming since their compilers output language files that are then compiled or interpreted. At the other just about no-one writes code to generate code in a language that they have built a compiler for. – Móż Nov 12 '13 at 21:08
  • Metaprogramming is ubiquitous. Any opensource project relying on Autotools is using metaprogramming. MOC in the Qt world, tblgen in LLVM, T4 in numerous .NET projects, pretty much any web project generating JavaScript, and so on. – SK-logic Nov 13 '13 at 10:19

4 Answers4

5

If you consider reflection a kind of meta-programming, it is relatively common. For some people even generic programming (templates and generics) is a form of meta-programming, so that's even more common.

In my opinion, however, meta-programming is something more complex, that involves actual code generation, and therefore quite uncommon, even in scripting languages.

Wizard79
  • 7,327
  • 2
  • 41
  • 76
  • Why template metaprogramming is not an "actual code generation"?!? C++ templates are Turing-complete, and you can generate arbitrary complex code. – SK-logic Nov 13 '13 at 10:20
  • @SK-logic: I mean new code being generated on demand at runtime. Templates are processed by the compiler, you can see them as a powerful kind of precompiler macro. – Wizard79 Nov 15 '13 at 12:53
  • why would you want to generate code in runtime? The most powerful forms of metaprogramming (like in Common Lisp, Racket, Template Haskell, etc.) are in compile-time. – SK-logic Nov 15 '13 at 13:00
3

I think it depends strongly on what language you use. Common Lisp people probably make a fair use of it - the Meta-Object Protocol allows for neat things transparent/orthogonal persistence, for instance - and Smalltalkers use it frequently.

Frank Shearar
  • 16,643
  • 7
  • 48
  • 84
2

It tends to show up commonly and consistently in Common Lisp projects. Check out OnLisp and Let Over Lambda for some explanation/insight.

If you're just after examples (and know CL), check out the Antiweb repository; there's a good deal of meta-programming going on under the covers.

Inaimathi
  • 4,884
  • 1
  • 26
  • 34
1

For the past two years the first bit of programming on a new task has almost always been writing code to write code. There's a ton of boilerplate associated with interactions with a database that can be simplified down to automatic script generation. Think of things where you need to query 3 tables to form an object or some such. Why write it yourself if there's 10 of these things that might change in the future?

wheaties
  • 3,584
  • 22
  • 23