This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)(Learn how and when to remove this template message)
Metaprogramming is a programming technique in which computer programs have the ability to treat other programs as their data. It means that a program can be designed to read, generate, analyze 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, in turn reducing development time. It also allows programs greater flexibility to efficiently handle new situations without recompilation.
Metaprogramming can be used to move computations from run-time to compile-time, to generate code using compile time computations, and to enable self-modifying code. The ability of a programming language to be its own metalanguage is called reflection. Reflection is a valuable language feature to facilitate metaprogramming.
Metaprogramming was popular in the 1970s and 1980s using list processing languages such as LISP. LISP hardware machines were popular in the 1980s and enabled applications that could process code. They were frequently used for artificial intelligence applications.
Metaprogramming enables developers to write programs and develop code that falls under the generic programming paradigm. 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 by allowing one to write code without the concern of specifying data types since they can be supplied as parameters when used.
Metaprogramming usually works in one of three ways.
- The first approach is to expose the internals of the run-time engine to the programming code through application programming interfaces (APIs) like that for the .NET IL emitter.
- The third approach is to step outside the language entirely. General purpose program transformation systems such as compilers, which accept language descriptions and 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. One can see this at work with Scheme and how it allows to tackle some limitations faced in C by using constructs that were part of the Scheme language itself to extend C.
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.
#!/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 over 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. Quines are generally of recreational or theoretical interest only.
One style of generative approach 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.
One usage of metaprogramming is to instrument programs in order to do dynamic program analysis.
Metaprogramming can be used to weave behavioral changes in a program, as done in aspect-oriented programming. For instance, metaprogramming can be used to inject feature flags or to explore possible patches for fixing bugs.
Some argue that there is a sharp learning curve to make complete use of metaprogramming features. Since metaprogramming gives more flexibility and configurability at runtime, misuse or incorrect use of the metaprogramming can result in unwarranted and unexpected errors that can be extremely difficult to debug to an average developer. It can introduce risks in the system and make it more vulnerable if not used with care. 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. Due to this, some believe that only high-skilled developers should work on developing features which exercise metaprogramming in a language or platform and average developers must learn how to use these features as part of convention.
Uses in programming languages
- Common Lisp and most Lisp dialects.
- Scheme hygienic macros
- Racket (programming language)
- Template Haskell
- Scala macros
- Clojure macros
The IBM/360 and derivatives had powerful macro assembler facilities that were often used to generate complete assembly language 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.
Other assemblers, such as MASM, also support macros.
Metaclasses are provided by the following programming languages:
- C "X Macros"
- C++ Templates
- Common Lisp, Scheme and most Lisp dialects by using the quasiquote ("backquote") operator.
The list of notable metaprogramming systems is maintained at List of Program Transformation Systems.
- 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
- Reflection (computer programming)
- Self-modifying code
- Source code generation
- Transcompiler (also known as transpilation)
- Very Large Scale Integration
- 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.
- Walker, Max. "The Art of Metaprogrmming in Java". New Circle. Retrieved 28 January 2014.
- Krauss, Aaron. "Programming Concepts: Type Introspection and Reflection". The Societa. Retrieved 14 September 2014.
- Joshi, Prateek. "What Is Metaprogramming? – Part 2/2". Perpetual Enigma. Retrieved 14 August 2014.
- 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.
- "Art of Metaprogramming".
- Durieux, Thomas; Cornu, Benoit; Seinturier, Lionel; Monperrus, Martin (2017). "Dynamic patch generation for null pointer exceptions using metaprogramming" (PDF). 2017 IEEE 24th International Conference on Software Analysis, Evolution and Reengineering (SANER). IEEE: 349–358. arXiv:1812.00409. doi:10.1109/SANER.2017.7884635. ISBN 978-1-5090-5501-2.
- Bicking, Ian. "The challenge of metaprogramming". IanBicking.org. Retrieved 21 September 2016.
- Terry, Matt. "Beware of Metaprogramming". Medium.com. Medium Corporation. Retrieved 21 August 2014.
- Through Common Lisp Object System's "Meta Object Protocol"
- Lisp (programming language) "Self-evaluating forms and quoting", quasi-quote operator.
- "LMS: Program Generation and Embedded Compilers in Scala". scala-lms.github.io. Retrieved 2017-12-06.
- Rompf, Tiark; Odersky, Martin (June 2012). "Lightweight Modular Staging: A Pragmatic Approach to Runtime Code Generation and Compiled DSLs". Commun. ACM. 55 (6): 121–130. doi:10.1145/2184319.2184345. ISSN 0001-0782.
- 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.