7

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?

Community
  • 1
  • 1
smdrager
  • 7,327
  • 6
  • 39
  • 49
  • 8
    Sound like a job for T4 to me... – Marc Gravell Sep 09 '11 at 17:32
  • 2
    C++ has templates that come close to what you are wanting: http://www.cplusplus.com/doc/tutorial/templates/ – Todd Moses Sep 09 '11 at 17:48
  • If your primary concern is the tediousness of entering the boilerplate (and not the possibility for changes in your boilerplate), Resharper has a nice Live Templates feature that really helps with this. T4 is more powerful (you can update the template definition and regenerate the file with the updated template), but it also adds some additional complexity. Vanilla VS has Code Snippets, which can also help. – Dan Bryant Sep 09 '11 at 17:51
  • 2
    Take a look at http://nemerle.org/About/ – SK-logic Sep 09 '11 at 19:15

2 Answers2

9

… 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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
9

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.

Julien Lebosquain
  • 40,639
  • 8
  • 105
  • 117