What exactly is metaprogramming?

后端 未结 7 2172
醉话见心
醉话见心 2020-12-12 09:00

I was reading an article on TheServerSide on ployglot programming on the Java platform. Some comments in the article refer to metaprogramming as the ability to generate code

相关标签:
7条回答
  • 2020-12-12 10:04

    Great question. I'm very sorry to see that none of the answers currently really answer your question correctly. Perhaps I can help...

    The definition of metaprogramming is really quite simple: it means programs that manipulate programs.

    Your accepted answer says programs that manipulate themselves. Those are indeed metaprograms but they are a subset of all metaprograms.

    All:

    • Parsers
    • Domain specific languages (DSLs)
    • Embedded domain specific languages (EDSLs)
    • Compilers
    • Interpreters
    • Term rewriters
    • Theorem provers

    are metaprograms. So the GCC compiler is a metaprogram, the CPython interpreter is a metaprogram, the Mathematica computer algebra system is a metaprogram, the Coq theorem prover is a metaprogram and so on.

    Other answers have asserted that metaprograms are programs that generate other programs. Those are indeed metaprograms but, again, they are a subset of all metaprograms. The Fastest Fourier Transform in the West (FFTW) library is an example of such a metaprogram. The source code is written mostly in OCaml and it generates bits of C code (called codelets) that are combined to create high performance Fast Fourier Transform routines optimised for specific machines. That library is actually used to provide the FFT routines in Matlab. People have been writing programs to generate numerical methods for decades, since the early days of FORTRAN.

    The first programming language that integrated support for metaprogramming was LISt Processor (LISP) language in late 1950s. LISP 1.5 included a number of features that made metaprogramming easier. Firstly, LISP's core data type is nested lists, i.e. trees like (a (b c) d), which means any LISP code can expressed natively as a data structure. This is known as homoiconicity. Secondly, LISP code can be converted into data easily using QUOTE. For example (+ 1 2 3) adds 1+2+3 and (QUOTE (+ 1 2 3)) creates an expression that adds 1+2+3 when evaluated. Thirdly, LISP provided a meta-circular evaluator that allows you to use the host interpreter or compiler to evaluate LISP code at run-time, including run-time generated LISP code. LISP's descendants include Scheme and Clojure. In all of these languages metaprogramming is most commonly seen in the form of programs that modify themselves, typically using macros.

    In the 1970s, Robin Milner developed a MetaLanguage (ML) that evolved into the ML family of programming languages which includes Standard ML and OCaml and strongly influenced Haskell and F#. These languages make it easy to express other languages. In these languages metaprograms are most commonly seen in the form of lexers, parsers, interpreters and compilers.

    In 1994, Erwin Unruh discovered that the C++ template system was Turing complete and could be used to execute arbitrary programs at compile time. C++ template metaprogramming brought metaprogramming to the unwashed masses who (ab)used it for many different things including generating numerical methods in the Blitz++ library.

    0 讨论(0)
提交回复
热议问题