Why does this not work anymore?

Philippe Sigaud philippe.sigaud at gmail.com
Sat Mar 23 05:12:21 PDT 2013


On Sat, Mar 23, 2013 at 12:57 PM, Martin <martinbbjerregaard at gmail.com> wrote:
> On Saturday, 23 March 2013 at 11:55:37 UTC, Andrej Mitrovic wrote:
>>
>> On Saturday, 23 March 2013 at 10:46:11 UTC, Martin wrote:
>>>
>>>    enum allMembers = __traits(allMembers, T);
>>
>>
>> Try:
>>      enum allMembers = [__traits(allMembers, T)];
>
>
> That actually works, thanks. I thought tuples were supposed be indexable
> though

They are, I think it's a bug.

If the alias grammar was more open, we could do:

alias allMembers = __traits(allMembers, T);

but since __traits are not accepted as an alias target, we must use a
wrapping template as a workaround:

import std.stdio;

struct TestStruct
{
    int a;
    int b;
    int c;
}

/// Just to hide __traits:
template Alias(aliases...)
{
    alias Alias = aliases;
}

template getSomeMember(T)
{

    alias members = Alias!(__traits(allMembers, T));

    // now members is a tuple of members
    // perfectly indexable & slicable
    enum getSomeMember = allMembers[0];

}

void main(string[] args)
{
    enum someMember = getSomeMember!TestStruct;
    writeln(typeof(someMember).stringof);
    writeln(someMember);
}


More information about the Digitalmars-d mailing list