template this and traits getOverloads issue.

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


Background:
===========

http://stackoverflow.com/questions/33764540/warning-about-overriden-methods-when-using-a-mixin

Why this horrible trick has to be used:
=======================================

cast this as (T) in a function template that's been mixed in an 
ancestor is not always usable, unless I miss something:

----
import std.stdio;

mixin template Bug()
{
     import std.traits;
     void bug(T)()
     {
         foreach(member; __traits(allMembers, T))
             foreach(overload; __traits(getOverloads, T, member))
         {
                 auto dg = &overload;
                 writeln(member);
         }
     }
}

class A
{
     mixin Bug;
     this(){bug!A;}
     void foo(){}
}

class B: A
{
     void bar(){}
     void bar(uint a){}
     this(){bug!B;}
}

void main(){
     new A;
     new B;
}
----


>a.d(11,27): Error: this for bar needs to be type B not type a.A
>a.d(11,27): Error: this for bar needs to be type B not type a.A
>a.d(11,27): Error: this for this needs to be type B not type a.A


everything that can be done to avoid the compilations errors will 
also prevent "bar" to be written in the output (because a B will 
never be analyzed). The "only" fix I see is like in the stack 
overflow answer: statically check if the mixin methods are 
already there and remix the mixin in each descendant, so that the 
getOverloads traits works on the right 'this'.

What do you think ? is it a bug ?


More information about the Digitalmars-d-learn mailing list