Skip to content

Metaprogramming

在文章Meta中,总结了programming language中的各种meta。

NOTE: meta是一种抽象

wikipedia Metaprogramming

Metaprogramming is a programming technique in which computer programs have the ability to treat 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. [1][2] In some cases, this allows programmers to minimize the number of lines of code to express a solution, thus reducing the development time.[3] It also allows programs greater flexibility to efficiently handle new situations without recompilation.

NOTE: 这一段中描述了metaprogramming的核心思想: "treat programs as their data";

结合使用C++、Python的经验来看,这是一种非常强大的能力,能够带来极大的灵活性,能够大大增加语言的power。

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 language in which the metaprogram is written is called the metalanguage. The language of the programs that are manipulated is called the attribute-oriented programming language. The ability of a programming language to be its own metalanguage is called reflection or "reflexivity".[4] Reflection is a valuable language feature to facilitate metaprogramming.

NOTE:

从metaprogramming的角度来看,我们编写的program就包含两类了:

classification of program
program 使用**programming language**
metaprogram 使用**metaprogramming language**

metaprogram是由language的实现来执行,下面是例子:

1) C++ metaprogram是由compiler来执行的(static)

2) Python metaprogram是由Python interpreter来执行的(dynamic)

第二段中的"Metaprogramming can be used to move computations from run-time to compile-time"是典型的compile time function execution,参见Theory\Compile-time-and-run-time章节。

大多数programming language的metalanguage和hosting language是相同的,比如:

C++和python都支持metaprogramming,它们的programming language和metaprogramming language是相同。具体到C++,它使用的template metaprogramming,所以它的template就属于metaprogram。

Approaches

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(泛型编程通过允许编写代码而不考虑指定数据类型来调用语言中的元编程工具,因为它们可以在使用时作为参数提供).

NOTE: 上面这段话描述了metaprogramming 和 generic programming之间的关系。显然metaprogramming是属于generic programming的。

Metaprogramming usually works in one of three ways.[5]

  1. 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(发射器).

  2. The second approach is dynamic execution of expressions that contain programming commands, often composed from strings, but can also be from other methods using arguments or context, like Javascript.[6] Thus, "programs can write programs." Although both approaches can be used in the same language, most languages tend to lean toward one or the other.

  3. 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.

Not all metaprogramming involves generative programming(产生式编程). If programs are modifiable at runtime or if incremental compilation is available (such as in C#, Forth, Frink, Groovy, JavaScript, Lisp, Elixir, Lua, Perl, PHP, Python, REBOL, Ruby, SAS, Smalltalk, and Tcl), then techniques can be used to perform metaprogramming without actually generating source code.

Uses in programming languages

NOTE:下面描述了使用metaprogramming的实现technique

Macro systems

Main article: Macro (computer science)

Macro assemblers

NOTE: 暂未了解

Metaclasses

Metaclasses are provided by the following programming languages:

Template metaprogramming

Main article: Template metaprogramming

NOTE: C++是使用template metaprogramming的典范,所以将template metaprogramming放到了C-family-language\C++\Language-reference\Template\Template-metaprogramming.md