3

We all know that MetaProgramming is a Concept of Code == Data (or programs that write programs).

But are there any applications that use it & what are the advantages of using it?

This Question can be closed but i didnt see any related questions.

Dan Rigby
  • 17,133
  • 6
  • 43
  • 60
Saif al Harthi
  • 2,948
  • 1
  • 21
  • 26

7 Answers7

8

IDEs are full with metaprogramming:

  • code completion
  • code generation
  • automated refactoring

Metaprogramming is often used to work around the limitations of Java:

  • code generation to work around the verbosity (e.g. getter/setter)
  • code generation to work around the complexity (e.g. generating Swing code from a WYSIWIG editor)
  • compile time/load time/runtime bytecode rewriting to work around missing features (AOP, Kilim)
  • generating code based on annotations (Hibernate)

Frameworks are another example:

  • generating Models, Views, Controllers, Helpers, Testsuites in Ruby on Rails
  • generating Generators in Ruby on Rails (metacircular metaprogramming FTW!)

In Ruby, you pretty much cannot do anything without metaprogramming. Even simply defining a method is actually running code that generates code.

Even if you just have a simple shell script that sets up your basic project structure, that is metaprogramming.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
4

Since code as data is one of key concepts of Lisp, the best thing would be to see the real applications of projects written in these.

On this link you can see an article about a real world application written partly in Clojure, a dialect of Lisp.

The thing is not to write programs that write programs, just because you can, but to add new functionality to your language when you really need it. Just think if you could simply add new keyword to Java or C#...

Goran Jovic
  • 9,418
  • 3
  • 43
  • 75
  • i understand that , but i saw a ruby guy saving some ruby code in a log file & then executes it again & he explained that he will use this method to build a music generator application or something so i started wondering about where metaprogramming can be used or being used in real life applications – Saif al Harthi Dec 16 '10 at 01:22
  • If you have ever used reflection in a real life application, then you did metaprogramming. What you are talking about is probably *code generation*. It is just one of metaprogramming techniques, and not the best one (though it could be perfect for a specific problem of music generator) – Goran Jovic Dec 16 '10 at 07:16
  • The "best" one? These are just technologies to manipulate code. There isn't any "best". Use the right tool for the right job. – Ira Baxter Dec 22 '10 at 10:30
  • @Ira: I stand corrected. I should have written, not *always* the best one. – Goran Jovic Dec 22 '10 at 13:26
2

Not a real world application, but a talk about metaprogramming in ruby:

Google TechTalks August 3, 2006 Jack Herrington, the author of Code Generation in Action (Manning, July 2003) , will talk about code generation techniques using Ruby. He will cover both do-it-yourself and off-the-shelf solutions in a conversation about where Ruby is as a tool, and where it's going.

A real world example would be Django's model metaclass. It is the class of the class, from which models inherit from and responsible for the outfit of the model instances with all their attributes and methods.

miku
  • 181,842
  • 47
  • 306
  • 310
  • i know ruby has the metaprogramming Capability , and also C# Compiler as a service will add metaprogramming to the language but i really think its used in real world applications – Saif al Harthi Dec 16 '10 at 01:12
2

If you implement metaprogramming in a language-independent way, you get a program analysis and transformation system. This is precisely a tool that treats (arbitrary) programs as data. These can be used to carry out arbitrary transformations on arbitrary programs.

It also means you aren't limited by the specific metaprogramming features that the compiler guys happened to put into your language. For instance, while C++ has templates, it has no "reflection". But a program transformation system can provide reflection even if the base langauge doesn't have it. In particular, having a program transformation engine means never having to say "I'm sorry, your language doesn't support metaprogramming (well enough) so I can't do much except write code manually".

See our DMS Software Reengineering Toolkit for such a program transformation system. It has been used to build test coverage and profiling tools, code generation tools, tools to reshape the architecture of large scale C++ applications, tools to migrate applications from one langauge to another, ... This is all extremely practical. Most of the tasks done with DMS would completely impractical to do by hand.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
1

Any ORM in a dynamic language is an instant example of practical metaprogramming. E.g. see how SQLAlchemy or Django's ORM creates classes for tables it discovers in the database, dynamically, in runtime.

ORMs and other tools in Java world that use @annotations to modify class behavior do a bit of metaprogramming, too.

9000
  • 39,899
  • 9
  • 66
  • 104
1

Metaprogramming in C++ allows you to write code that will get transformed at compilation.

There are a few great examples I know about (google for them):

  • Blitz++, a library to write efficient code for manipulating arrays
  • Intel Array Building Blocks
  • CGAL
  • Boost::spirit, Boost::graph
Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
0

Many compilers and interpreters are implemented with metaprogramming techniques internally - as a chain of code rewriting passes.

ORMs, project templates, GUI code generation in IDEs had been mentioned already.

Domain Specific Languages are widely used, and the best way to implement them is to use metaprogramming.

Things like Autoconf are obviously cases of metaprogramming.

Actually, it's unlikely one can find an area of software development which won't benefit from one or another form of metaprogramming.

SK-logic
  • 9,605
  • 1
  • 23
  • 35