Alex Rivera | Logout

Is metaprogramming a subset of reflection?

Asked 2011-10-03T23:41:53.140
22

I used to think that metaprogramming involved modifying the program, and (as do some answers to What is reflection and why is it useful? ) that reflection merely consisted of introspection of a program. However, the reflection tag wiki says

Reflection is the process by which a program can observe and modify its own structure and behavior at runtime.

Reflection is the process by which a program can perform introspection. This introspection usually involves the ability to observe and modify its own structure and behavior at runtime. From theoretical perspective reflection relates to the fact that program instructions are stored as data. The distinction between program code and data is a matter of how the information is treated. Hence programs can treat their own code as data and observe or modify them.

[emphasis added]

And the description for metaprogramming is

Metaprogramming is writing programs that write or manipulate other programs as their data.

Metaprogramming is useful because it can save programmers valuable time. Some languages have support to metaprogram themselves and this allows to create code with great expressive power.

(I assume that "write" doesn't mean writing source code to a file, because that'd be code generation.)

Would this make metaprogramming merely a subset of reflection?

Or are the terms distinct because some programming languages are metaprogrammed by another language, in which case metaprogramming occurs but not reflection? (There was a single uncited sentence claiming this in the metaprogramming Wikipedia article)

Or do the terms "reflection" and "metapr

Edit
Report

1 Answer

4

Reflection can also happen during execution, when for instance a program is able to inspect its own call stack (by looking into call frames inside) And metaprogramming could even change the functions which have been called (that is which have frames lower in the call stack) together with their call frames.

You might consider reading Jacques Pitrat's book on Artificial Beings - the conscience of a conscious machine which explain in details why this is useful and how it can be implemented. See also his blog.

answered 2011-10-22T21:08:35.650

Your Answer