Request: traits isImplemented

Ali Çehreli acehreli at yahoo.com
Fri Jul 5 10:28:31 PDT 2013


On 07/05/2013 02:52 AM, JS wrote:
> I have a request to add to __traits to check if a method/property of a
> class/struct is actually implemented or not.
>
> interface A { void myfunc(); }
>
> class B : A { } // myfunc not implemented
> class C : A { void myfunc() { ... } // myfunc implemented
>
> This may seem odd but I need it for conditional mixin compilation to add
> in the methods when they are not implemented. A sort of default proxy,
> wrapper, or composite pattern implementer. It reduces boilerplate code
> significantly.
>
> Unfortunately allMembers returns methods that are not actually
> implemented or part of the class.
>
> Something like allImmediateMembers would be useful where it only returns
> the actually explicit members of the immediate class/struct that it is
> used on and accomplish the same task.
>
>

Can you make A a class with default myfunc() implementation?

class A {
     void myfunc()
     {
         writeln("default myfunc");
     }
}

class B : A { }
class C : A { override void myfunc() { writeln("C.myfunc"); } }

Or, you can leave A as an interface and insert another class after it 
that has the default implementation:

     A
     |
ADefaults
  |    |
  B    C

Ali



More information about the Digitalmars-d mailing list