Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
March 23, 2013 Why does this not work anymore? | ||||
---|---|---|---|---|
| ||||
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 |
March 23, 2013 Re: Why does this not work anymore? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin | On Saturday, 23 March 2013 at 10:46:11 UTC, Martin wrote:
> enum allMembers = __traits(allMembers, T);
Try:
enum allMembers = [__traits(allMembers, T)];
|
March 23, 2013 Re: Why does this not work anymore? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | 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
|
March 23, 2013 Re: Why does this not work anymore? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin | 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); } |
March 23, 2013 Re: Why does this not work anymore? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | 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
|
Copyright © 1999-2021 by the D Language Foundation