Meta-programming - 0. Introduction

In all my years programming some times I have been working with metaprogramming on several programming languages and using different approaches. And I would like to use all the knowledge adquired through this time, to write an article about this issue.

Wikipedia quote:

Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at compile time that would otherwise be done at runtime. In many cases, this allows programmers to get more done in the same amount of time as they would take to write all the code manually, or it gives programs greater flexibility to efficiently handle new situations without recompilation.
The language in which the metaprogram is written is called the metalanguage. The language of the programs that are manipulated is called the object language. The ability of a programming language to be its own metalanguage is called reflection or reflexivity.
On most of the programming languages there are some ways to make use of metaprogramming:
  • In C, C++ and assembler there is a preprocessor (macros) (metalanguage)
  • In C++, D, Java and C# you have templates and generic types
  • In D you have template mixins and pure functions that can be executed on compiling time
  • In PHP, ActionScript 3, Java and C# you have Reflexion
  • In Python, Squirrel, ActionScript 3, Java and C# you have Annotations or Custom Attributes
  • In PHP and Javascript you can use eval or includes to dynamically-generated files (templating systems)
  • In C#, Java and Actionscript (and all the other languages running in a Virtual Machine) you can emit bytecodes of that Virtual Machine and execute them
  • On lots of programming languages, there is a way to generate native code and execute it JIT (Just In Time) and dynarec (Dynamic Recompilation).
  • In C#, J# and other languages, you can access the code of delegates as abstract trees (Expression Trees). Very useful for LINQ (Language INtegrated Query)

I will write about those meta-programming topics in coming articles. Their uses, good points and problems and practical cases where I have used them.

Metaprogramming is one of the most advanced topics on programming, so these coming articles are targeted to the more experienced programmers.