Is there any language out there which uses code templating?
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 properti开发者_Python百科es 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?
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.
… 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.
精彩评论