www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why does this not work anymore?

reply "Martin" <martinbbjerregaard gmail.com> writes:
module main;

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

template getSomeMember(T)
{

     enum allMembers = __traits(allMembers, T);
     enum getSomeMember = allMembers[0];

}

void main(string[] args)
{

     enum someMember = getSomeMember!TestStruct;

}

Gives me the following erorrs:
main.d(13): Error: variable _allMembers_field_0 cannot be read at 
compile time
main.d(21): Error: template instance 
main.getSomeMember!(TestStruct) error instantiating

Running dmd 2.062
Mar 23 2013
parent reply "Andrej Mitrovic" <andrej.mitrovich gmail.com> writes:
On Saturday, 23 March 2013 at 10:46:11 UTC, Martin wrote:
     enum allMembers = __traits(allMembers, T);
Try: enum allMembers = [__traits(allMembers, T)];
Mar 23 2013
parent reply "Martin" <martinbbjerregaard gmail.com> writes:
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
Mar 23 2013
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Sat, Mar 23, 2013 at 12:57 PM, Martin <martinbbjerregaard 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); }
Mar 23 2013
parent "Martin" <martinbbjerregaard gmail.com> writes:
On Saturday, 23 March 2013 at 12:12:30 UTC, Philippe Sigaud wrote:
 On Sat, Mar 23, 2013 at 12:57 PM, Martin 
 <martinbbjerregaard 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); }
I see, thanks a lot
Mar 23 2013