www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - UDA and mixins

reply "Oleg" <code.viator gmail.com> writes:
I want to write mixin template, what past and/or change some code 
in class. Mixin must get all functions with user defined 
attribute and past fields for example. It should looks like this

struct FooPasted(Args...){}

class A
{
mixin foo;

void func1()  mark { ... }
void func2( int x, string a )  mark { ... }

}

Question 1:

I get members with  mark via this template:

template filterMembers(alias F,T,string[] list=[])
if( is( T == class ) || is( T == struct ) )
{
     static if( list.length > 0 ) enum filterMembers = impl!list;
     else enum filterMembers = impl!([__traits(allMembers,T)]);

     string[] impl(string[] names)()  property
     {
         static if( names.length == 0 ) return [];
         else static if( names.length == 1 )
         {
             enum n = names[0];
             static if( __traits(compiles,__traits(getMember,T,n)) 
)
                 return F!(__traits(getMember,T,n)) ? [n] : [];
             else return [];
         }
         else return impl!(names[0..$/2]) ~ impl!(names[$/2..$]);
     }
}

an easier way to do this exists? something like 
__traits(UDAMembers,T,mark)

Question 2:

Can i change or remove exists code via mixin?

definition

class X
{
   mixin foo;
   void func()  impl;
}

change to

class X
{
   void func() { ... implement by foo ... }
}
Dec 16 2014
parent "Oleg" <code.viator gmail.com> writes:
struct FooPasted(Args...){}

class A
{
mixin foo;

void func1()  mark { ... }
void func2( int x, string a )  mark { ... }
}

must change to:

class A
{
void func1()  mark { ... }
void func2( int x, string a )  mark { ... }

FooPasted!() func1_mark;
FooPasted!(int,string) func2_mark;
}
Dec 16 2014