问题描述
我曾经认为元编程涉及修改程序,并且(对),反射仅由程序的内省组成.但是,反射标签维基说
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 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.
[已添加重点]
元编程的描述是
元编程非常有用,因为它可以节省程序员宝贵的资源 时间.一些语言本身支持元程序,这 允许创建具有强大表达力的代码.
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?
或者这些术语是不同的,因为某些编程语言是由另一种语言进行元编程的,在这种情况下会发生元编程,而不是反射? (元编程Wikipedia文章中只有一个未引用的句子声称这一点)
还是根据个人使用的编程语言,反射"和元编程"这两个术语的用法有所不同?
推荐答案
No. Rather, reflection provides facilities that are a subset of what metaprogramming can do.
元编程是编写程序的程序".这包括读取程序文本(可以说包括它们自己,但很少见),分析代码并进行更改的程序.是的,它包括将源文本写入文件.代码生成是元编程的一种特殊情况.
如果您将语言设计者和语言设计者内置的限制集扩展到 之外,您将获得更强大的反射"功能.一个真正好的元编程系统可以访问整个程序结构,因此可以回答有关程序结构的任意问题(模态图灵限制).
例如,我们的 DMS软件再造工具包是一种程序转换工具,可以完全访问程序的抽象语法树以及由各种 DMS语言前端派生的其他事实.因此DMS可以任意地反映"(检查/分析/原因)正在处理的语言.它可以对C,COBOL,Java,C#和C ++做到这一点;对于这些语言中的许多语言,它不仅可以访问AST,而且还可以访问符号表信息以及各种形式的控制和数据流,而我所见过的反射功能都无法为您提供这种功能.
As an example, our DMS Software Reengineering Toolkit is a program transformation tool that has complete access to the abstract syntax tree of the program and many other facts derived by the various DMS language front ends. So DMS can "reflect" (inspect/analyze/reason) rather arbitrarily about the language which it is processing. And it can do so for C, COBOL, Java, C# and C++; for many of these langauges, it can not only provide access to the AST, but access to symbol table information and various forms of control and data flow, which no reflection facilities I've ever seen offer you.
此外,像DMS这样的程序转换工具还可以基于反射"来修改代码,以生成新代码,优化,重组,使用仪器……通过这种方式可获得的各种效果是令人惊讶的广泛.
Additionally, a program transformation tool like DMS can modify the code based on the "reflection" to generate new code, optimize, restructure, instrument, ... The variety of effects achievable this way is surprisingly broad.
[由于DMS是作为一组DSL实现的,因此它实际上可以并且确实会(关于反射")其自己的代码.我们使用DMS从其DSL合成自身的大部分内容,包括代码生成以及一些非常有趣的优化,包括工作并行化.]
[Since DMS is implemented as set of DSLs, it in fact can and does reason ("reflect") about its own code. We use DMS to synthesize large parts of itself from its DSLs, including code generation with some pretty interesting optimizations, including working parallelization.]
这篇关于元编程是反射的子集吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!