有没有使用代码模板的语言?
是否有任何语言具有代码模板形式?让我解释一下我的意思...我今天正在开发一个 C# 项目,其中我的一个类非常重复,是一系列属性 getter 和 setter。
public static int CustomerID
{
get
{
return SessionHelper.Get<int>("CustomerID", 0); // 0 is the default value
}
set
{
SessionHelper.Set("CustomerID", value);
}
}
public static int BasketID
{
get
{
return SessionHelper.Get<int>("BasketID", 0); // 0 is the default value
}
set
{
SessionHelper.Set("BasketID", value);
}
}
... and so forth ...
我意识到这基本上可以分解为类型、名称和默认值。
我看到这篇文章,和我设想的差不多,但是没有参数的余地(默认)。
但我在想,很多时候代码会分解为模板。
例如,语法可以是这样的:
public template SessionAccessor(obj defaultValue) : static this.type this.name
{
get
{
return SessionHelper.Get<this.type>(this.name.ToString(), defaultValue);
}
set
{
SessionHelper.Set(this.name.ToString(), value);
}
}
public int CustomerID(0), BasketID(0) with template SessionAccessor;
public ShoppingCart Cart(new ShoppingCart()) with template SessionAccessor; // Class example
我觉得这在编写简洁、DRY 代码方面有很多可能性。这种类型的事情在 C# 中可以通过反射来实现,但是速度很慢,并且应该在编译期间完成。
那么问题来了:任何现有的编程语言都可以实现这种类型的功能吗?
Is there any language which has a form of code templating? Let me explain what I mean... I was working on a C# project today in which one of my classes was very repetitive, a series of properties getters and setters.
public static int CustomerID
{
get
{
return SessionHelper.Get<int>("CustomerID", 0); // 0 is the default value
}
set
{
SessionHelper.Set("CustomerID", value);
}
}
public static int BasketID
{
get
{
return SessionHelper.Get<int>("BasketID", 0); // 0 is the default value
}
set
{
SessionHelper.Set("BasketID", value);
}
}
... and so forth ...
I realize that this could break down into basically a type, a name, and a default value.
I saw this article, which is similar to what I envision, but has no room for parameters (the default).
But I was thinking, there are many times where code breaks down into templates.
For example, the syntax could go as such:
public template SessionAccessor(obj defaultValue) : static this.type this.name
{
get
{
return SessionHelper.Get<this.type>(this.name.ToString(), defaultValue);
}
set
{
SessionHelper.Set(this.name.ToString(), value);
}
}
public int CustomerID(0), BasketID(0) with template SessionAccessor;
public ShoppingCart Cart(new ShoppingCart()) with template SessionAccessor; // Class example
I feel like this would have a lot of possibilities in writing succinct, DRY code. This type of thing would be somewhat achievable in c# with reflection, however that is slow and this should done during the compile.
So, question: Is this type of functionality possible in any existing programming language?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Marc Gravell 评论的那样,对于 T4(文本模板转换工具包),它是集成在 Visual Studio 中的模板处理器,可以与 C# 或 VB 一起使用,并且可以生成任何内容。它是一个工具,而不是内置的语言功能。
将文本模板文件 (.tt) 添加到您的项目中,该模板将非常简单:
T4 非常适合此类代码生成。我强烈推荐 T4 Toolbox 轻松为每个模板生成多个文件,访问 EnvDTE 直接在 Visual 中解析现有的 C# 代码工作室和其他优点。
As Marc Gravell commented, it's an easy job for T4 (Text Template Transformation Toolkit), which is a templating processor integrated inside Visual Studio, that can be used with C# or VB and can generate anything. It's a tool, not a built-in language feature though.
Add a Text Template file (.tt) to your project, the template will be as simple as:
T4 are great for this kind of code generation. I highly recommend T4 Toolbox to easily generate multiple files per template, access EnvDTE to parse your existing C# code directly inside Visual Studio and other goodness.
...并且您已经发现了元编程的奇妙世界。欢迎! :-)
原型元编程语言是 Lisp,或者任何其他可以在代码中表示其自身结构的语言。
其他语言已经尝试在某种程度上复制这一点; C 中的宏就是一个突出的例子。
最近在某种程度上支持这一点的著名语言候选是 C++ 通过其 >模板和Ruby。
… aand you have discovered the wonderful world of metaprogramming. Welcome! :-)
The archetypal metaprogramming language is Lisp, or really any other language that can represent its own structure in code.
Other languages have tried copying this to some extent; macros in C are a prominent example.
More recent prominent candidates of languages that support this to some extent are C++ via its templates, and Ruby.