Metaprogramming is the writing of programs that write or manipulate other programs (or themselves) as their data or that do part of the work that is otherwise done at compile time during 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.
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 capacity of a programming language to be its own meta-language is called reflexivity.
A simple example of a metaprogram is this bash script, which is an example of generative programming:
#!/bin/bash # metaprogram echo '#!/bin/bash' >program for ((I=1; I<=992; I++)); do echo "echo $I" >>program done chmod +x program
This script (or program) generates a new 993 line program which prints out the numbers 1–992. This is only an illustration on how to use code to write more code, not the most efficient way to print out a list of numbers. Nonetheless, a good programmer can write and execute this metaprogram in just a couple of minutes, and will have generated exactly 1000 lines of code in that amount of time.
Not all metaprogramming involves generative programming. If programs are modifiable at runtime (as in Lisp, Python, Smalltalk, Ruby, PHP, and other languages), then techniques can be used to accomplish metaprogramming without actually generating source code.
Reflection is a valuable language feature for facilitating metaprogramming. Having the programming language itself as a first class data type (as in Lisp) is also very useful.
Code generation | Programming paradigms
Metaprogrammierung | Metaprogramación | Métaprogrammation | メタプログラミング | Metaprogramowanie | Метапрограммирование | Lập trình meta
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Metaprogramming".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world