UDA and mixins
Oleg via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Dec 16 06:24:37 PST 2014
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 ... }
}
More information about the Digitalmars-d-learn
mailing list