digitalmars.D.learn - What exactly is the use-case for Template This Parameters?
- Andrej Mitrovic (4/4) Feb 18 2013 http://dlang.org/template.html#TemplateThisParameter
- Steven Schveighoffer (20/24) Feb 18 2013 I actually use it in dcollections :)
- Andrej Mitrovic (2/3) Feb 18 2013 Ah, that sheds a big light on this feature actually, thanks.
- Lee Braiden (23/28) Feb 23 2013 I guess it's for doing things like:
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.
Feb 18 2013
On Mon, 18 Feb 2013 17:14:54 -0500, Andrej Mitrovic <andrej.mitrovich gmail.com> 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 actually use it in dcollections :) What it's nice for is simulating covariance with interface templates: interface Addable(T) { auto addAll(this R)(T[] stuff) { foreach(t; stuff) add(t); return cast(R)this; } Addable add(T t); } class List(T) : Addable!(T) { List add(T t) { ... } // don't need to redefine addAll } This became important when operators were forced to be templates. -Steve
Feb 18 2013
On 2/19/13, Steven Schveighoffer <schveiguy yahoo.com> wrote:// don't need to redefine addAllAh, that sheds a big light on this feature actually, thanks.
Feb 18 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
Feb 23 2013