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?
A templating language basically is a language which allows defining placeholders that should later on be replaced for the purpose of implementing designs. Obviously modern template languages not only support placeholders, but also loops and conditions which are often necessary for designing a web page.
The HTML Template Language is easy to learn and its features are purposely limited to ensure that it stays simple and straight-forward. It also has powerful mechanisms for structuring the markup and invoking logic, while always enforcing strict separation of concerns between markup and logic.
The main type of templates that can be implemented in C are static templates. Static templates are created at compile time and do not perform runtime checks on sizes, because they shift that responsibility to the compiler.
PHP is not a template engine, but a language that can be used to write templates, or template engines. A template engine is not just a language, but also the programming API that allows the scripts to locate, organize templates or assign the data from the script to them.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With