On 2013-07-23 06:27, Paul O'Neil wrote:
I'm trying to write some code that finds all the members of a class that
have a particular user defined attribute.  My current attempt is at
https://github.com/todayman/d_template_experiments/tree/8fccd27d7d5557ec6e2f0614374cf5f79fe80b4c


I would like to have a static method that returns an array of strings of
the names of the members with the "@Sync" attribute.  I use the
allMembers trait to get the list of strings, then try to filter out the
ones I want.  There are problems when I convert from the string to a
symbol / something to get the attributes.  Right now, I'm getting errors
like:

I tried your code and this should be enough:

import std.typetuple;

static bool doesFieldSync (string field) ()
{
alias attrs = TypeTuple!(__traits(getAttributes, mixin("FileData." ~ field)));
    return staticIndexOf!(Sync, attrs) != -1;
}

The important things here are that __traits(getAttributes) returns a tuple. You cannot assign a tuple to a variable. But you can alias it. When you alias it you do need to use this wrapper TypeTuple, I don't remember why but that's how it works.

// This is the method I'm trying to write
static string[] syncableFields() {
    auto result = new string[0];
    foreach( key ; __traits(derivedMembers, FileData) )
    {
        if( doesFieldSync!key() ) {
            result ~= key;
        }
    }
    return result;
}

Here I used derivedMembers instead of allMembers, this is want you want, most times.

But I see that there are some problems with some members. These members are mixed in "mixin Signal". So if you try to filter out these members. I would just hard code them.

--
/Jacob Carlborg

Reply via email to