元编程 - 自解释代码 - 教程,文章,书籍 [英] Metaprogramming - self explanatory code - tutorials, articles, books
问题描述
我正在寻找类似于白痴的指南(免费书籍下载,在线资源)。此外,我想要的比你的平均wiki页面和一些语言不可知或最好用Java示例。
你知道这样的资源,将允许有效地把所有的(我知道经验有很多话要说,但我想要建立经验,避免流的坏决定 - 经验 - 好的决定)?
EDIT:
此示例中的实用程序员:
...实现一个迷你语言来控制一个简单的绘图包...该语言由单字母命令组成。某些命令后面跟着一个数字。例如,以下输入将绘制一个矩形:
P 2#选择笔2
D# b $ b W 2#draw west 2cm
N 1#then north 1
E 2#then east 2
S 1#then back south
U#pen up
谢谢!
欢迎来到元编程的奇妙世界:)元编程涉及许多事情。我会尽量列出我的想法:
-
宏。首先在术语宏下探索了扩展编程语言的语法和语义的能力。有几种语言具有类似于宏的结构,但是选择项当然是 Lisp 。如果你对元编程感兴趣,理解Lisp和宏系统(以及代码和数据具有相同表示的同步性本质)是必须的。如果你想在JVM上运行一个Lisp方言,去 Clojure 。少量资源:
Lisp。
-
DSL 。扩展一种语言语法和语义的能力现在在术语DSL下被重新标记。创建DSL的最简单方法是使用解释器模式。然后内部DSL 与流畅接口和外部DSL(根据Fowler的术语)。这是我最近看过的一段漂亮的视频:
>其他答案已指出此区域的资源。 -
反射。元编程也是不可分割的反射。在运行时反映程序结构的能力是非常强大的。因此,了解自省, intercession and 合作。 IMHO,反射允许两个大类的事情:1.操作其结构在编译时不知道的数据(然后在运行时提供数据的结构,并且程序静态反映)。 2.强大的编程模式,例如动态代理,工厂等。 Smalltalk 是探索反射的选择,其中 是反射的。但我认为 Ruby 也是一个很好的候选者,有一个社区利用元编程(但我自己不太了解Ruby)。
还有丰富的反思文献。
-
注释。注释可以看作是一种语言的反射能力的一个子集,但我认为它应该有自己的类别。我已经回答了一次什么注释是什么,以及如何使用。注释是可以在编译时或运行时处理的元数据。 Java 对注释处理器工具,可插拔注释处理API ,以及镜像API 。
-
字节代码或AST转换。这可以在编译时或在运行时完成。这是一种低级方法,但也可以被认为是元编程的一种形式(在某种意义上,它与非同构语言的宏相同。)
- DSL with Groovy (最后有一个示例,说明如何插入自己的 AST转换与注释)。
结论:元程序是程序对自身进行推理或修改自身的能力。就像元栈溢出本身是提出问题堆栈溢出本身的地方。元编程不是一种特定的技术,而是一系列的概念和技术。
一些事情属于元程序设计的范畴。从你的问题,你似乎对宏/ DSL部分更感兴趣。但一切都是最终相关的,所以元编程的其他方面也是绝对值得一看。
PS:我知道我提供的大多数链接不是教程或介绍性文章。这些是我喜欢的资源,描述元编程的概念和优势,我认为更有趣
I am looking into improving my programming skils (actually I try to do my best to suck less each year, as our Jeff Atwood put it), so I was thinking into reading stuff about metaprogramming and self explanatory code.
I am looking for something like an idiot's guide to this (free books for download, online resources). Also I want more than your average wiki page and also something language agnostic or preferably with Java examples.
Do you know of such resources that will allow to efficiently put all of it into practice (I know experience has a lot to say in all of this but i kind of want to build experience avoiding the flow bad decisions - experience - good decisions)?
EDIT:
Something of the likes of this example from the Pragmatic Programmer:
...implement a mini-language to control a simple drawing package... The language consists of single-letter commands. Some commands are followed by a single number. For example, the following input would draw a rectangle:
P 2 # select pen 2
D # pen down
W 2 # draw west 2cm
N 1 # then north 1
E 2 # then east 2
S 1 # then back south
U # pen up
Thank you!
Welcome to the wonderful world of meta-programming :) Meta programming relates actually to many things. I will try to list what comes to my mind:
Macro. The ability to extend the syntax and semantics of a programming language was explored first under the terminology macro. Several languages have constructions which resemble to macro, but the piece of choice is of course Lisp. If you are interested in meta-programming, understanding Lisp and the macro system (and the homoiconic nature of the languge where code and data have the same representation) is definitively a must. If you want a Lisp dialect that runs on the JVM, go for Clojure. A few resources:
There is otherwise plenty of resource about Lisp.
DSL. The ability to extend one language syntax and semantics is now rebranded under the term "DSL". The easiest way to create a DSL is with the interpreter pattern. Then come internal DSL with fluent interface and external DSL (as per Fowler's terminology). Here is a nice video I watched recently:
The other answers already pointed to resources in this area.
Reflection. Meta-programming is also inseparable form reflection. The ability to reflect on the program structure at run-time is immensely powerful. It's important then to understand what introspection, intercession and reification are. IMHO, reflection permits two broad categories of things: 1. the manipulation of data whose structure is not known at compile time (the structure of the data is then provided at run-time and the program stills works reflectively). 2. powerful programming patterns such as dynamic proxy, factories, etc. Smalltalk is the piece of choice to explore reflection, where everything is reflective. But I think Ruby is also a good candidate for that, with a community that leverage meta programming (but I don't know much about Ruby myself).
- Smalltalk: a reflective language
- Magritte: a meta driven approach to empower developpers and end-users
There is also a rich literature on reflection.
Annotations. Annotations could be seen as a subset of the reflective capabilities of a language, but I think it deserves its own category. I already answered once what annotations are and how they can be used. Annotations are meta-data that can be processed at compile-time or at run-time. Java has good support for it with the annotation processor tool, the Pluggable Annotation Processing API, and the mirror API.
Byte-code or AST transformation. This can be done at compile-time or at run-time. This is somehow are low-level approach but can also be considered a form of meta-programming (In a sense, it's the same as macro for non-homoiconic language.)
- DSL with Groovy (There is an example at the end that shows how you can plug your own AST transformation with annotations).
Conclusion: Meta-programming is the ability for a program to reason about itself or to modify itself. Just like meta stack overflow is the place to ask question about stack overflow itself. Meta-programming is not one specific technique, but rather an ensemble of concepts and techniques.
Several things fall under the umbrella of meta-programming. From your question, you seem more interested in the macro/DSL part. But everything is ultimately related, so the other aspects of meta-programming are also definitively worth looking at.
PS: I know that most of the links I've provided are not tutorials, or introductory articles. These are resources that I like which describe the concept and the advantages of meta-programming, which I think is more interesting
这篇关于元编程 - 自解释代码 - 教程,文章,书籍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!