有没有使用代码模板的语言?

发布于 2024-12-03 16:42:03 字数 1435 浏览 1 评论 0原文

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

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

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

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

我觉得这在编写简洁、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).

Generic Property in C#

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扬花落满肩 2024-12-10 16:42:03

正如 Marc Gravell 评论的那样,对于 T4(文本模板转换工具包),它是集成在 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 中解析现有的 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:

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

指尖微凉心微凉 2024-12-10 16:42:03

...并且您已经发现了元编程的奇妙世界。欢迎! :-)

原型元编程语言是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文