|This article needs additional citations for verification. (August 2011) (Learn how and when to remove this template message)|
Metaprogramming is the art of writing computer programs with the ability to treat programs as their data. It means that a program could be designed to read, generate, analyse or transform other programs, and even modify itself while running. In some cases, this allows programmers to minimize the number of lines of code to express a solution (hence reducing development time), or it gives programs greater flexibility to efficiently handle new situations without recompilation. One of the types of metaprogramming involves only writing their program using generic programming.
Metaprogramming is used to move the computations from the run-time to compile-time, enable self-adapting code and generate code using compile time computations.
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.
Reflection is a valuable language feature to facilitate metaprogramming. Having the programming language itself as a first-class data type (as in Lisp, Prolog, SNOBOL, or Rebol) is also very useful; this is known as homoiconicity. Generic programming invokes a metaprogramming facility within a language, in those languages supporting it.
The third way is to step outside the language entirely. General purpose program transformation systems such as compilers, which accept language descriptions and can carry out arbitrary transformations on those languages, are direct implementations of general metaprogramming. This allows metaprogramming to be applied to virtually any target language without regard to whether that target language has any metaprogramming abilities of its own.
In statically typed functional languages
The IBM/360 and derivatives had powerful assembler macro facilities that were often used to generate complete programs or sections of programs (for different operating systems for instance). Macros provided with CICS transaction processing system had assembler macros that generated COBOL statements as a pre-processing step.
- Objective C
#!/bin/sh # metaprogram echo '#!/bin/sh' >program for I in $(seq 992) do echo "echo $I" >> program done chmod +x program
This script (or program) generates a new 993-line program that prints out the numbers 1–992. This is only an illustration of how to use code to write more code; it is not the most efficient way to print out a list of numbers. Nonetheless, a programmer can write and execute this metaprogram in less than a minute, and will have generated exactly 1000 lines of code in that amount of time.
A quine is a special kind of metaprogram that produces its own source code as its output.
Lisp is probably the quintessential language with metaprogramming facilities, both because of its historical precedence and because of the simplicity and power of its metaprogramming. In Lisp metaprogramming, the unquote operator (typically a comma) introduces code that is evaluated at program definition time rather than at run time; see Self-evaluating forms and quoting in Lisp. The metaprogramming language is thus identical to the host programming language, and existing Lisp routines can be directly reused for metaprogramming, if desired.
This approach has been implemented in other languages by incorporating an interpreter in the program, which works directly with the program’s data. There are implementations of this kind for some common high-level languages, such as RemObjects’ Pascal Script for Object Pascal.
One style of metaprogramming is to employ domain-specific languages (DSLs). A fairly common example of using DSLs involves generative metaprogramming: lex and yacc, two tools used to generate lexical analyzers and parsers, let the user describe the language using regular expressions and context-free grammars, and embed the complex algorithms required to efficiently parse the language.
Challenges of metaprogramming
We have seen that metaprogramming can help us to give more flexibility and configurability at runtime. However the wrong use of the metaprogramming can result in unwarranted and unexpected errors. Some of the common problems which can occur due to wrong use of metaprogramming are inability of the compiler to identify missing configuration parameters, invalid or incorrect data can result in unknown exception or different results
- Aspect weaver
- Comparison of code generation tools
- Compile-time reflection
- Genetic programming
- Inferential programming
- Instruction set simulator
- Intentional Programming
- Interpreted language
- Machine learning
- Partial evaluation
- Self-modifying code
- Source code generation
- Source-to-source compilation: automated translation from one programming language to another
- Template metaprogramming
- Course on Program Analysis and Transformation. By Prof. Harald Sondergaard."Course on Program Analysis and Transformation". Retrieved 18 September 2014.
- Czarnecki, Krzysztof; Eisenecker, Ulrich W. (2000). Generative Programming. ISBN 0-201-30977-7.
- for example, instance_eval in Ruby takes a string or an anonymous function. "Rdoc for Class: BasicObject (Ruby 1.9.3) - instance_eval". Retrieved 30 December 2011.
- Chlipala, Adam (June 2010). "Ur: statically-typed metaprogramming with type-level record computation" (PDF). ACM SIGPLAN Notices. PLDI '10. 45 (6): 122–133. doi:10.1145/1809028.1806612. Retrieved 29 August 2012.
- https://medium.com/@macterry/beware-of-metaprogramming-3afdc931cadf#.tca0seuot. Missing or empty