元编程是反射的子集吗? [英] Is metaprogramming a subset of reflection?

查看:87
本文介绍了元编程是反射的子集吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经认为元编程涉及修改程序,并且(对

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.

[已添加重点]

元编程的描述是

元编程正在编写编写或操纵其他程序的程序 程序作为数据.

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?

或者这些术语是不同的,因为某些编程语言是由另一种语言进行元编程的,在这种情况下会发生元编程,而不是反射? (元编程Wikipedia文章中只有一个未引用的句子声称这一点)

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 "metaprogramming" get used differently depending on what programming language the person is using?

推荐答案

否.相反,反射提供的功能是元编程可以做什么的子集.

No. Rather, reflection provides facilities that are a subset of what metaprogramming can do.

元编程是编写程序的程序".这包括读取程序文本(可以说包括它们自己,但很少见),分析代码并进行更改的程序.是的,它包括将源文本写入文件.代码生成是元编程的一种特殊情况.

Metaprogramming is "programs which write programs". This includes programs that read the text of programs (arguably including themselves but that is rather rare), analyze that code, and make changes. Yes, it includes writing source text to files. Code generation is a special case of metaprogramming.

据我了解,反思是程序查询其自身结构的能力.在我所见过的几乎每个系统中,都有可能进行反射(在Lisp和等效变体的非常特殊的情况下),反射机制仅提供了有限的自省方式. Java和C#可以让您找出类和方法的名称,但是您不能向这些系统询问方法,语句或局部声明的内容.您也不能要求大多数这样的反射语言实际更改其结构,也就是说,您不能使用反射工具来添加新的类或字段.大多数语言(例如C ++)基本上没有内置的反射"功能.尽管内置在语言中的反射实用程序可能有用,但它们在语言设计者/编译器构建者决定在运行时保留的内容方面往往是特质的.

Reflection as I understand it is the ability for a program to inquire about its own structure. In virtually every system I have seen in which reflection is possible (with the really exceptional case of Lisp and equivalent variants), the reflection machinery provided only a limited means of introspection. Java and C# will let you find out the names of classes and methods, but you cannot ask these systems for the content of a method, statement or local declaration. Nor can you ask most such reflective langauges to actually change their structure, that is, you can't add new classes or fields using the reflection facilities. Most langauges (e.g., C++) have basically no built-in ability to "reflect". While the reflection utilities built into langauges can be useful, they tend to be idiosyncratic with respect to what the language designers/compiler builders decided to keep around at runtime.

如果您将语言设计者和语言设计者内置的限制集扩展到 之外,您将获得更强大的反射"功能.一个真正好的元编程系统可以访问整个程序结构,因此可以回答有关程序结构的任意问题(模态图灵限制).

You end up with a much more powerful "reflection" capability if you step outside the language and the set of restrictions the langauge designers built into it. A really good metaprogramming system has access to the entire program structure, and thus can answer arbitrary questions about the program structure (modulo Turing limitations).

例如,我们的 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.]

这篇关于元编程是反射的子集吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆