Metaprogramming is a programming technique in which code writes other code or
introspects upon itself. The prefix meta- (from Greek) refers to abstraction; code that
uses metaprogramming techniques works at two levels of abstraction simultaneously.
2 | Chapter 1: Foundational Techniques
Metaprogramming is used in many languages, but it is most popular in dynamic languages
because they typically have more runtime capabilities for manipulating code as
data. Though reflection is available in more static languages such as C# and Java, it is
not nearly as transparent as in the more dynamic languages such as Ruby because the
code and data are on two separate levels at runtime.
Introspection is typically done on one of two levels. Syntactic introspection is the lowest
level of introspection??”direct examination of the program text or token stream.
Template-based and macro-based metaprogramming usually operate at the syntactic
level.
Lisp encourages this style of metaprogramming by using S-expressions (essentially a
direct translation of the program??™s abstract syntax tree) for both code and data.
Metaprogramming in Lisp heavily involves macros, which are essentially templates
for code. This offers the advantage of working on one level; code and data are both
represented in the same way, and the only thing that distinguishes code from data is
whether it is evaluated.
Pages:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25