CSC/ECE 517 Fall 2010/ch2 S24 sk

From Expertiza_Wiki
Jump to navigation Jump to search

What is Metaprogramming

Metaprogramming is in essence using programs to manipulate other programs. the concept is actually a classic UNIX principle on writing code and is far from new. In fact, its technical beginning probably starts with Konrad Zuse's insight almost 70 years ago that a computer could prepare its own instructions. Metaprogramming has been garnering the attention of developers and software engineers in recent years, first with dynamic languages such as Ruby and more recently with static languages such as Java and C++.
Metaprogramming is also called generative programming. The idea is that instead of modifying simple data elements, you modify symbols (or patterns) that represent complex operations. This is achieved through three key components. A metalanguage where we define a generalization or pattern. A generator which is an expression of that generalization and finally an instance which is the output from the evaluation of that generator. The goal is to generalize a set of concrete instances that can be evaluated and generated as a static representation of what we wish our code to accomplish. This takes polymorphism variation out of the code level and moves it up to the metalevel where it can be interpreted by Domain Specific Languages (DSLs) which describe how a system should be generated.
Some key applications of metaprogamming are the translation of a program from one language to another, to transform a program , to refactor a program, to optimize a program, verification of a program, type checking a program, and to apply design patters to a program.

Metaprogramming in Practice

Before we elaborate to much on metaprogramming, here is some fundamental terminology to become familiar with.
  • A Metaprogram represents a program that has the ability to modify another program or itself
  • Reflection is the ability of a program to manipulate the state of a program during execution. This manipulation comes in two aspects: introspection and intercession
  • Introspection is the ability of a program to observe and reason about its own state
  • Intercession is the ability of a program to alter its own execution state or alter its own interpretation or meaning
  • A Metaobject represents an object during reflection as well as execution stacks, the processor, and nearly all elements of the language and its exectution environment
  • Metalevel Architecture provides information about selected system properties that helps enables generated code members to become self aware
  • Partial Evaluation Evaluating a system while one part is varying or in the process of generation and another is constant
Even though metaprogramming in static languages has been getting a lot of attention from software creators recently, the capability has been around quite some time. In C for example, the preprocessor acts as the vehicle for generating code. This type of metaprogramming was mostly limited to simple macros due to the preprocessors lack of recursion and lack of type checking. This made generating code rather dangerous and complicated (a trait that has plagued metaprogramming). Still, it showed the need was there and as time went on the tools for building metaprograms became more robust.

Drawbacks of Metaprogramming

Addressing the Drawbacks

Reflex Structural Model

Conclusions

References