template this and traits getOverloads issue.

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 20 07:03:01 PST 2015


On Friday, 20 November 2015 at 14:18:00 UTC, Alex Parrill wrote:
> But you don't need a template for this case; mixin templates 
> have access to `this`:

Indeed, this is a good answer too.

The difference between this and the template thing I did is that 
yours is virtual so calling it through an interface will work 
even on subclasses. However, you must mix it into each sub class.

Mine is a template that only needs to be in the base 
class/interface, but also need a `this` of the derived type to 
see the derived type. (That's why I ran `this.bug();` in the 
constructor of B btw) If you call it on an variable typed as the 
interface, it will only show interface members.

IF i = new A();
i.bug(); // would only show interface members with mine

A a = new A();
a.bug(); // will now show A's members too

That's what the template this parameter does: the type of this at 
the *usage site* is passed as the parameter.


With your solution, the type of this at the *mixin site* is 
available.



I think your solution is generally better for stuff like 
serialization where you are passed an interface but need child 
members too. The template this param I used is nice for interface 
functions that need some kind of covariance; returning a type 
based on how it was used.


More information about the Digitalmars-d-learn mailing list