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?