What exactly is the use-case for Template This Parameters?

Lee Braiden leebraid at gmail.com
Sat Feb 23 15:39:59 PST 2013


On Mon, 18 Feb 2013 23:14:54 +0100, Andrej Mitrovic wrote:

> http://dlang.org/template.html#TemplateThisParameter
> 
> That's hardly a descriptive example, so in what context is the feature
> useful? Some code snippets would be welcome so we can update the page
> with a nicer and more useful example.

I guess it's for doing things like:

import std.stdio;

class Mammal {
    void printDescription(this T)()
    {
        writeln(typeid(T));
    }
};

void main(string[] args)
{
    class Dog : Mammal {};
    class Cat : Mammal {};

    auto a = new Dog();
    auto b = new Cat();

    a.printDescription();
    b.printDescription();
}


Outputs:

hello.main.Dog
hello.main.Cat


-- 
Lee


More information about the Digitalmars-d-learn mailing list