元编程到底是什么? [英] What exactly is metaprogramming?

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

问题描述

我正在通过 Java平台上的ployglot编程来阅读有关TheServerSide的文章. .本文中的一些评论将元编程称为生成代码的能力(也许是在运行中).

I was reading an article on TheServerSide on ployglot programming on the Java platform. Some comments in the article refer to metaprogramming as the ability to generate code (perhaps on the fly).

是在元编程中即时生成代码的能力,还是在运行时将方法和属性注入到现有对象中的能力(例如Python,Ruby和Groovy等某些动态语言所允许的能力).

Is metaprogramming the ability to generate code on the fly or is it the ability to inject methods and attributes into existing objects at runtime (like what some dynamic languages like Python, Ruby, and Groovy allow).

推荐答案

元编程是指程序具有自身知识或可以操纵自身的多种方式.

Metaprogramming refers to a variety of ways a program has knowledge of itself or can manipulate itself.

在像C#这样的语言中,反射是元编程的一种形式,因为该程序可以检查有关其自身的信息.例如,返回对象的所有属性的列表.

In languages like C#, reflection is a form of metaprogramming since the program can examine information about itself. For example returning a list of all the properties of an object.

在像ActionScript这样的语言中,您可以在运行时评估函数以创建新程序,例如eval("x" + i).当我为1时,DoSomething()会影响一个名为x1的对象,而当我为2时,DoSomething()会影响一个名为x2的对象.

In languages like ActionScript, you can evaluate functions at runtime to create new programs such as eval("x" + i). DoSomething() would affect an object called x1 when i is 1 and x2 when i is 2.

最后,元编程的另一种常见形式是程序可以以非平凡的方式更改自身. LISP以此闻名,是Paul Graham大约十年前所倡导的东西.我将不得不查阅他的一些具体论文.但是想法是程序将根据其状态更改程序的另一部分.这提供了一定程度的灵活性,可以在运行时进行决策,这在当今大多数流行语言中都是非常困难的.

Finally, another common form of metaprogramming is when the program can change itself in non-trivial fashions. LISP is well known for this and is something Paul Graham championed about a decade ago. I'll have to look up some of his specific essays. But the idea is that the program would change another part of the program based on its state. This allows a level of flexibility to make decisions at runtime that is very difficult in most popular languages today.

值得注意的是,在以直接汇编进行编程的美好时光中,在运行时更改自身的程序是必要且非常普遍的.

It is also worth noting that back in the good ol' days of programming in straight assembly, programs that altered themselves at runtime were necessary and very commonplace.

保罗·格雷厄姆(Paul Graham)的文章什么使Lisp与众不同" :

From Paul Graham's essay "What Made Lisp Different":

许多语言都有一种叫做 宏.但是Lisp宏是唯一的.和 信不信由你,他们所做的是 与括号有关.这 Lisp的设计师并没有把所有这些 语言中的括号只是 不同的.对于Blub程序员, Lisp代码看起来很奇怪.但是那些 出现括号是有原因的. 它们是 Lisp之间的根本区别 和其他语言.

Many languages have something called a macro. But Lisp macros are unique. And believe it or not, what they do is related to the parentheses. The designers of Lisp didn't put all those parentheses in the language just to be different. To the Blub programmer, Lisp code looks weird. But those parentheses are there for a reason. They are the outward evidence of a fundamental difference between Lisp and other languages.

Lisp代码由Lisp数据组成 对象.而不是琐碎的意义 源文件包含 字符和字符串是其中之一 语言支持的数据类型. Lisp代码,由 解析器,由数据结构组成 您可以遍历.

Lisp code is made out of Lisp data objects. And not in the trivial sense that the source files contain characters, and strings are one of the data types supported by the language. Lisp code, after it's read by the parser, is made of data structures that you can traverse.

如果您了解编译器的工作方式, 真正发生的事情并不多 Lisp的语法很奇怪 Lisp没有语法.你写程序 在生成的解析树中 在编译器内部时,其他 语言被解析.但是这些解析 树木完全可供您使用 程式.您可以编写程序 操纵他们.在Lisp中,这些 程序称为宏.他们是 编写程序的程序.

If you understand how compilers work, what's really going on is not so much that Lisp has a strange syntax as that Lisp has no syntax. You write programs in the parse trees that get generated within the compiler when other languages are parsed. But these parse trees are fully accessible to your programs. You can write programs that manipulate them. In Lisp, these programs are called macros. They are programs that write programs.

编写程序的程序?什么时候 你会想要这样做吗?不是 如果您在Cobol中思考的话,就会非常频繁.全部 时间,如果您考虑使用Lisp.它 如果可以的话在这里会很方便 举一个强大的宏的例子, 在那里说!那个怎么样?但是如果 我做到了,看起来就像 对不认识的人胡言乱语 Lisp;这里没有空间可以解释 您需要知道的一切 了解它的意思.在 Ansi 常见的Lisp 我试图移动东西 尽我所能,甚至如此 直到第160页,我才开始使用宏.

Programs that write programs? When would you ever want to do that? Not very often, if you think in Cobol. All the time, if you think in Lisp. It would be convenient here if I could give an example of a powerful macro, and say there! how about that? But if I did, it would just look like gibberish to someone who didn't know Lisp; there isn't room here to explain everything you'd need to know to understand what it meant. In Ansi Common Lisp I tried to move things along as fast as I could, and even so I didn't get to macros until page 160.

但是我想我可以给 可能令人信服的论点.这 Viaweb编辑器的源代码是 大概约20-25%的宏.巨集 比普通的Lisp难写 功能,它被认为是 在不使用它们时使用它们的不良风格 必要的.因此,该代码中的每个宏 在那里,因为它必须存在.什么 这意味着至少有20%到25% 该程序中的代码正在执行 在任何情况下都无法轻松完成的事情 其他语言.对此表示怀疑 Blub程序员可能与我有关 声称拥有的神秘力量 Lisp,这应该使他感到好奇. 我们没有为我们编写此代码 自己的娱乐.我们是一家很小的创业公司, 尽我们最大的努力来编程 为了设置技术壁垒 在我们和我们的竞争对手之间.

But I think I can give a kind of argument that might be convincing. The source code of the Viaweb editor was probably about 20-25% macros. Macros are harder to write than ordinary Lisp functions, and it's considered to be bad style to use them when they're not necessary. So every macro in that code is there because it has to be. What that means is that at least 20-25% of the code in this program is doing things that you can't easily do in any other language. However skeptical the Blub programmer might be about my claims for the mysterious powers of Lisp, this ought to make him curious. We weren't writing this code for our own amusement. We were a tiny startup, programming as hard as we could in order to put technical barriers between us and our competitors.

可疑的人可能开始 想知道是否存在一些相关性 这里.我们代码的很大一部分是 做很难做的事情 用其他语言.所结果的 软件做了我们竞争对手的事情 软件做不到.也许有 某种联系.我鼓励 您遵循该主题.也许有 对那个ho脚的老人更感兴趣 cru着拐杖,胜过眼睛.

A suspicious person might begin to wonder if there was some correlation here. A big chunk of our code was doing things that are very hard to do in other languages. The resulting software did things our competitors' software couldn't do. Maybe there was some kind of connection. I encourage you to follow that thread. There may be more to that old man hobbling along on his crutches than meets the eye.

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

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