哪种编程语言具有最好的自省和反思能力? [英] Which programming language has the best introspection and reflection capabilities?

查看:132
本文介绍了哪种编程语言具有最好的自省和反思能力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多语言都具有内省和反思的能力. 但是,有些显然比其他的要好. 关于哪种语言在这方面更好"的任何建议,以及原因.

Many languages have the ability for introspection and reflection. However, some are clearly better than others. Any suggestions as to which languages are "better" in this respect, and why.

推荐答案

反射本质上是提取源程序属性(或描述事实的属性)的能力.为了能够检查关于源代码的任意属性(例如,任何和所有属性),要求您具有可检查的源代码或至少一些等效的源代码.

Reflection is essentially the ability to extract properties of (or facts describing) the source program. To be able to inspect arbitrary properties (e.g, any and all) about the source code requires that you have the source code available to inspect, or at least some source code equivalent.

允许运行时反射的语言预先确定了可以检查的许多属性中的哪一个;语言的数量和产生的细节已由语言设计师确定.由于提供反射需要花费运行时间和时间,因此实际上所有这些语言(特定于语言)都对可反射/检查的内容有所限制,因为它们都不愿意保留源代码的全部内容.因此,它们反映了语言设计师认为可能有用的内容,并且可以相对轻松地进行计算并紧凑地存储它们.这些限制对于编译后的语言尤其重要,因为编译后的语言的目标是高效地执行和节省资源(与解释器相对).

Languages which allow runtime reflection have predetermined which of many properties can be inspected; the number and resulting details have been decided by the language designers. Because it costs runtime space and time to provide reflection, virtually all such languages have (language specific) limits on what can be reflected/inspected, because none of them are willing to retain the source code in its entirety. So they reflect what the language designers think might be useful, and what can be computed relatively easily and stored compactly. These limitations are especially strong for compiled languages, whose goals are to be efficient in execution and resources (as opposed to interpreters).

因此,您通常无法反映和获取有关注释的信息,或有关特定变量可能具有的值范围的信息.

Thus you cannot typically reflect and get information about comments, or information about the range of values that a particular variable might hold.

LISP是提供大量反射功能的一种语言.这是因为LISP程序源文本在设计上与LISP列表同构,这就是LISP至少在解释时是如何存储程序代码的. LISP提供对所有此类列表的直接访问,因此LISP程序可以直接检查其自己的代码. [许多LISP允许编译;就检查对象的限制而言,它们与其他所有编译语言都具有相同的限制.]

One of the languages that provides quite a lot of reflective ability is LISP. This is because LISP program source texts are by design isomorphic to LISP lists, which is how LISP stores the program code, at least when interpreted. LISP provides direct access to all such lists, and thus LISP programs can inspect their own code directly. [Many LISPs allow compilation; these have the same limitations of every other compiled language in terms of limitation on what can be inspected].

因此,一种语言的内省/反思能力在实践中总是会受到限制.

So a language's ability to introspect/reflect will always be limited in practice.

如果要完全访问源文本的任意属性,则需要将语言移至 外,以便实际上可以直接检查完整的源代码.

If you want to have full access to arbitrary properties of the source text, you need to step outside the language, so that you can in effect directly inspect the full source code.

程序转换系统(PTS)是与此最接近的工具. PTS通常会将源代码解析为抽象语法树,这些语法树直接反映了源代码,因此可以充当等效的替代品.更好的替代品实际上保留有关空白的注释和信息.然后可以使用 some PTS编写自定义的任意算法,以遍历AST并从这些AST中提取任意信息.

Program transformation systems (PTS) are the tools that come closest to this. PTSs generally parse the source code into abstract syntax trees which are a direct reflection of the source code, and can thus act as an equivalent stand in. The better ones actually retain comments and information about the whitespace. One can then write custom, arbitrary, algorithms with some PTS to climb over the ASTs and extract arbitrary information from those ASTs.

使用PTS的一个非常好的副作用之一是,在检查了代码之后,PTS还可以转换代码或生成其他代码,以帮助解决驱动反映需求的问题.也就是说,分析问题或机会,并因此增强程序.

One of the very nice side effects of using a PTS, is that after having inspected the code, the PTS can also transform the code or generate additional code to help resolve the question driving the need to reflect. That is, analyze for issues or opportunities, and enhance the program as a result.

也许梦想的语言将是内置完整的PTS的语言. (我公司的DMS产品PTS是一小套具有此属性的协作DSL,我们经常使用DMS进行检查/转换和永久增强自身.通常,我们使用DMS检查/转换其他语言.)

Perhaps the dream language would be one which has a full PTS built into it. (My company's DMS product, a PTS, is an small set of cooperating DSLs that has this property, and we often use DMS to inspect/transform and enhance itself permanently. Normally we DMS in inspect/transform other languages.).

这篇关于哪种编程语言具有最好的自省和反思能力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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