gpt4 book ai didi

metaprogramming - 元编程到底是什么?

转载 作者:行者123 更新时间:2023-12-03 04:41:31 30 4
gpt4 key购买 nike

我正在阅读 TheServerSide 上的一篇文章 ployglot programming on the Java platform 。文章中的一些评论将元编程称为生成代码的能力(可能是动态生成的)。

元编程是动态生成代码的能力,还是在运行时将方法和属性注入(inject)现有对象的能力(就像 Python、Ruby 和 Groovy 等动态语言所允许的那样)。

最佳答案

元编程是指程序了解自身或操纵自身的多种方式。

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

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

最后,元编程的另一种常见形式是程序可以以非平凡的方式改变自身。 LISP 以此而闻名,也是 Paul Graham 大约十年前所倡导的。我得查一下他的一些具体论文。但其想法是,程序将根据其状态更改程序的另一部分。这使得在运行时做出决策具有一定程度的灵 active ,这在当今大多数流行语言中是非常困难的。

还值得注意的是,在直接汇编编程的美好时代,在运行时改变自身的程序是必要的并且非常普遍。

来自 Paul Graham 的文章 "What Made Lisp Different" :

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

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.

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.

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.

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.

关于metaprogramming - 元编程到底是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/514644/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com