digitalmars.D - Why does this not work anymore?
- Martin (22/22) Mar 23 2013 module main;
- Andrej Mitrovic (3/4) Mar 23 2013 Try:
- Martin (3/7) Mar 23 2013 That actually works, thanks. I thought tuples were supposed be
- Philippe Sigaud (31/42) Mar 23 2013 They are, I think it's a bug.
- Martin (2/50) Mar 23 2013 I see, thanks a lot
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
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
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:That actually works, thanks. I thought tuples were supposed be indexable thoughenum allMembers = __traits(allMembers, T);Try: enum allMembers = [__traits(allMembers, T)];
Mar 23 2013
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: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); }On Saturday, 23 March 2013 at 10:46:11 UTC, Martin wrote:That actually works, thanks. I thought tuples were supposed be indexable thoughenum allMembers = __traits(allMembers, T);Try: enum allMembers = [__traits(allMembers, T)];
Mar 23 2013
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:I see, thanks a lotOn Saturday, 23 March 2013 at 11:55:37 UTC, Andrej Mitrovic wrote: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); }On Saturday, 23 March 2013 at 10:46:11 UTC, Martin wrote:That actually works, thanks. I thought tuples were supposed be indexable thoughenum allMembers = __traits(allMembers, T);Try: enum allMembers = [__traits(allMembers, T)];
Mar 23 2013