One of the most under-used programming techniques is
writing programs that generate programs or program parts. Learn why
metaprogramming is necessary and look at some of the components of
metaprogramming (textual macro languages, specialized code generators).
See how to build a code generator and get a closer look at
language-sensitive macro programming in Scheme.
<script type="text/javascript"><!----></script>
<!---->
<!---->
<!---->
<!---->
Code-generating programs are sometimes called metaprograms
; writing such programs is called metaprogramming
.
Writing programs that write code has numerous applications.
This article explains why you might consider metaprogramming and
looks at some of the components of this art -- we'll dive into textual
macro languages, survey specialized code generators and discuss how to
build them, and dissect language-sensitive macro programming with
Scheme.
Many different uses for metaprogramming
First, you can write programs that will pre-generate tables of data
for use at runtime. For example, if you are writing a game and want a
quick lookup table for the sine of all 8-bit integers, you can either
calculate each sine yourself and hand-code it, have your program build
the table at startup at runtime, or write a program to build the custom
code for the table before compile-time. While it may make sense to
build the table at runtime for such a small set of numbers, other such
tasks may cause program startup to be prohibitively slow. In such
cases, writing a program to build static data tables is usually your
best answer.
Second, if you have a large application where many of the functions
include a lot of boilerplate code, you can create a mini-language that
will do the boilerplate code for you and allow you to code only the
important parts. Now, if you can, it's best to abstract out the
boilerplate portions into a function. But often the boilerplate code
isn't so pretty. Maybe there's a list of variables to be declared in
every instance, maybe you need to register error handlers, or maybe
there are several pieces of the boilerplate that have to have code
inserted in certain circumstances. All of these make a simple function
call impossible. In such cases, it is often a good idea to create a
mini-language that allows you to work with your boilerplate code in an
easier fashion. This mini-language will then be converted into your
regular source code language before compiling.
Finally, a lot of programming languages make you write really
verbose statements to do really simple things. Code-generating programs
allow you to abbreviate such statements and save a lot of typing, which
also prevents a lot of mistakes because there is less chance of
mistyping.
As languages acquire more features, code-generating programs get
less appealing. What is available as a standard feature of one language
may be available only through a code-generating program in another
language. However, inadequate language design is not the only reason
for needing code-generating programs. Easier maintenance is also a
reason.
A good toturial for Groovy in metaprogamming is:
http://www.javaworld.com/javaworld/jw-09-2008/jw-09-dsls-in-java-4.html#resources
分享到:
相关推荐
Metaprogramming is one of Elixir's greatest features. Maybe you've played with the basics or written a few macros. Now you want to take it to the next level. This book is a guided series of ...
### C++ 模板元编程 (Template Metaprogramming) 相关知识点 #### 一、书籍概述 《C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond》是由 David Abrahams 和 Aleksey ...
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond By David Abrahams, Aleksey Gurtovoy Publisher : Addison Wesley Professional Pub Date : December 10, 2004 ISBN ...
《C++ Template Metaprogramming Concepts, Tools, and Techniques from Boost and Beyond》是关于C++元编程的一本经典教材,适合那些对软件构造有深入兴趣的读者。元编程是C++语言的一个强大特性,它允许程序员在...
标题提到了"The Boost C++ Metaprogramming",意味着这篇文档关注的是在C++领域中,Boost库提供的模板元编程工具和相关技术。 描述部分强调了文档将讨论Boost C++模板元编程库,这是一个可扩展的编译时框架,包含...
### Metaprogramming in .NET #### 知识点概览 - **元编程概念**:定义、目的、实现方式。 - **反射技术**:基本原理、应用领域、操作示例。 - **文本模板转换工具包 (T4)**:工作流程、应用场景、模板语法介绍。 -...
内容简介 本书是关于C++模板元编程的著作。本书主要介绍Traits和类型操纵、深入...附赠光盘内容包含所有Boost C++ 程序库:Boost Metaprogramming Library及其参考文件,还包含所有随书代码示例以及大量的补充材料。
这本《Metaprogramming Ruby》书籍深入探讨了如何利用Ruby的特性进行元编程,帮助开发者提升代码的灵活性、可扩展性和复用性。源代码提供了书中各个示例的实践,让读者能够更好地理解元编程的概念。 元编程的核心...
Metaprogramming,也就是元编程,是一种在运行时或编译时修改程序结构的技术,它能提升代码的灵活性和自适应性。在.NET环境中,元编程可以帮助开发者创建更加动态和智能的软件系统。 元编程的核心概念在于“代码...
《Addison.Wesley.C++.Template.Metaprogramming.Concepts.Tools.and.Techniques》这本书深入探讨了C++模板元编程这一高级编程技术。模板元编程是C++语言中的一个独特领域,它允许程序员在编译时执行计算,极大地...
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond,《C++模板元编程》英文版。C++ In-Depth系列丛书之一,C++程序员必备。
### Metaprogramming Ruby:掌握如专业人士般的编程技巧 #### 一、引言 《Metaprogramming Ruby: Programming Like the Ruby Pros》是一本深入探讨Ruby元编程技术的专业书籍,作者Paolo Perrotta通过丰富的实例和...
这是C++ Template Metaprogramming原书附带的例子代码,可以方便各位进行研习,请首先编译开篇的第一个例子,以测试你手头的编译器对模板的支持程度。 另,有的亲反映上载的书无法打开,可能是Windows7进行了权限...
Addison Wesley - C++ Template Metaprogramming Concepts Tools And Techniques From Boost And Beyond
C++.Template.Metaprogramming.-.companion.cd.rar
本资源——"C++ Template Metaprogramming"——提供了一个深入探讨这一领域的英文版指南。 模板是C++语言中的一个关键特性,它允许我们定义函数和类的通用模板,然后在不同类型的参数上实例化它们。模板元编程则是...
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond
C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond By David Abrahams, Aleksey Gurtovoy Publisher : Addison Wesley Professional Pub Date : December...