Thread overview | |||||
---|---|---|---|---|---|
|
February 18, 2013 What exactly is the use-case for Template This Parameters? | ||||
---|---|---|---|---|
| ||||
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. |
February 18, 2013 Re: What exactly is the use-case for Template This Parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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
|
February 18, 2013 Re: What exactly is the use-case for Template This Parameters? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 2/19/13, Steven Schveighoffer <schveiguy@yahoo.com> wrote:
> // don't need to redefine addAll
Ah, that sheds a big light on this feature actually, thanks.
|
Copyright © 1999-2021 by the D Language Foundation