Which programming language has the best introspection and reflection capabilities? [closed]

岁酱吖の 提交于 2019-12-11 06:28:31

问题


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.


回答1:


Reflection is essentially the ability to extract properties of 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.

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.

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.

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.

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.).



来源:https://stackoverflow.com/questions/40562294/which-programming-language-has-the-best-introspection-and-reflection-capabilitie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!