【问题标题】:Is there any language out there which uses code templating?有没有使用代码模板的语言?
【发布时间】:2011-11-13 23:35:46
【问题描述】:

是否有任何具有代码模板形式的语言?让我解释一下我的意思...我今天正在处理一个 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 ...

我意识到这基本上可以分解为一个类型、一个名称和一个默认值。

看到这篇文章,和我预想的差不多,但是没有参数的余地(默认)。

Generic Property in C#

但我在想,很多时候代码会分解成模板。

例如,语法可以这样:

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

我觉得这在编写简洁、干燥的代码方面有很多可能性。这种类型的事情在 c# 中通过反射可以实现,但是速度很慢,应该在编译期间完成。

那么,问题:任何现有的编程语言都可以实现这种类型的功能吗?

【问题讨论】:

  • 对我来说听起来像是 T4 的工作......
  • C++ 的模板接近你想要的:cplusplus.com/doc/tutorial/templates
  • 如果您主要关心的是输入样板文件的乏味(而不是更改样板文件的可能性),Resharper 有一个很好的实时模板功能,它确实有助于解决这个问题。 T4 更强大(您可以更新模板定义并使用更新的模板重新生成文件),但它也增加了一些额外的复杂性。 Vanilla VS 有代码片段,也可以提供帮助。

标签: c# templates programming-languages metaprogramming


【解决方案1】:

正如 Marc Gravell 评论的那样,T4 (Text Template Transformation Toolkit) 是一个简单的工作,它是一个集成在 Visual Studio 中的模板处理器,可以与 C# 或 VB 一起使用,并且可以生成任何东西。不过,它是一个工具,而不是内置的语言功能。

将文本模板文件 (.tt) 添加到您的项目中,模板将非常简单:

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".generated.cs" #>
<#
var properties = new[] {
    new Property { Type = typeof(int), Name = "CustomerID", DefaultValue = 0 },
    new Property { Type = typeof(int), Name = "BasketID", DefaultValue = 0 },
};
#>
namespace YourNameSpace {
    public partial class YourClass {
<# foreach (Property property in properties) { #>
        public static <#= property.Type.FullName #> <#= property.Name #> {
            get { return SessionHelper.Get<<#= property.Type.FullName #>>("<#= property.Name #>", <#= property.DefaultValue #>); }
            set { SessionHelper.Set("<#= property.Name #>", value); }
        }
<# } #>
    }
}
<#+
public class Property {
    public Type Type { get; set; }
    public string Name { get; set; }
    public object DefaultValue { get; set; }
}
#>

T4 非常适合这种代码生成。我强烈推荐T4 Toolbox 轻松地为每个模板生成多个文件,访问 EnvDTE 以直接在 Visual Studio 中解析您现有的 C# 代码以及其他优点。

【讨论】:

【解决方案2】:

……你已经发现了metaprogramming 的奇妙世界。欢迎! :-)

原型元编程语言是Lisp,或者实际上是可以在代码中表示其自身结构的任何其他语言。

其他语言在某种程度上尝试过复制它; macros in C 就是一个突出的例子。

在一定程度上支持这一点的最近比较突出的候选语言是 C++ via its templatesRuby

【讨论】:

    猜你喜欢
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多