Skip to content

Commit

Permalink
Merge pull request #300 from AndrejMitrovic/TemplateThis
Browse files Browse the repository at this point in the history
Add a better Template This feature example.
  • Loading branch information
alexrp committed Mar 9, 2013
2 parents 0165e3e + b4087e1 commit d72d782
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions template.dd
Expand Up @@ -408,6 +408,61 @@ S
immutable(S)
)

$(P This is especially useful when used with inheritance. For example,
you might want to implement a final base method which returns a derived
class type. Typically you would return a base type, but this won't allow
you to call or access derived properties of the type:)

---
interface Addable(T)
{
final auto add(T t)
{
return this;
}
}

class List(T) : Addable!T
{
List remove(T t)
{
return this;
}
}

void main()
{
auto list = new List!int;
list.add(1).remove(1); // error: no 'remove' method for Addable!int
}
---

$(P Here the method $(B add) returns the base type, which doesn't implement the
$(D remove) method. The $(D template this) parameter can be used for this purpose:)

---
interface Addable(T)
{
final R add(this R)(T t)
{
return cast(R)this; // cast is necessary, but safe
}
}

class List(T) : Addable!T
{
List remove(T t)
{
return this;
}
}

void main()
{
auto list = new List!int;
list.add(1).remove(1); // ok
}
---

$(H2 Template Value Parameters)

Expand Down

0 comments on commit d72d782

Please sign in to comment.