template this and traits getOverloads issue.

BBasile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 20 12:07:41 PST 2015


On Friday, 20 November 2015 at 15:03:06 UTC, Adam D. Ruppe wrote:
> 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.

I review what I said before. Actually it doesn't work that well.

Using the two methods (yours or A.Parrill's one) the protected 
members in a class that's located in another module are not 
accessible, the relationship to the current class the traits code 
is ran into is totally lost and only public members are visible.

when bug() delcaration is `static void bug(T)(T t)`, pointer to 
members are get using __traits(getMember, t, member) and in your 
solution using _this instead of t. So it's a more for a less.


More information about the Digitalmars-d-learn mailing list