digitalmars.D.learn - How to iterate getSymbolsByUDA
- Eko Wahyudin (24/24) Nov 23 2018 Hi all,
- Stanislav Blinov (23/25) Nov 24 2018 enum Attr;
- Eko Wahyudin (5/32) Nov 24 2018 aah ya,, this statement is work
- bauss (12/51) Nov 24 2018 It should work with just:
- Eko Wahyudin (3/25) Nov 26 2018 seems my problem is i use GDC, i got this error
- Adam D. Ruppe (4/6) Nov 26 2018 The common versions of gdc don't support static foreach, but
- Steven Schveighoffer (7/16) Nov 26 2018 Yeah, the original code used for, not foreach. for does not work on
- Eko Wahyudin (12/12) Nov 27 2018 static foreach(sym; getSymbolsByUDA!(A, Attr)){
- Steven Schveighoffer (15/28) Nov 28 2018 Generally, foreach establishes a variable for the item, whereas static
Hi all, anyone know how to iterate getSymbolsByUDA ? I try this code: enum Attr; struct A { Attr int a; int b; } //------------------------------------------- //1. Error: need `this` for `a` of type `int` foreach(sym; getSymbolsByUDA!(A, Attr)){ writeln(sym.stringof); } for(sizediff_t i=0; i<getSymbolsByUDA!(A, Attr).length; i++){ //----------------------------------------------------- //2. error: variable `i` cannot be read at compile time writeln(getSymbolsByUDA!(A, Attr)[i].stringof); } //----------------------------------------------------- //3. this OK, but the index is a constant, i don't like it. writeln(getSymbolsByUDA!(A, Attr)[0].stringof); Thankyou before
Nov 23 2018
On Saturday, 24 November 2018 at 03:48:12 UTC, Eko Wahyudin wrote:Hi all, anyone know how to iterate getSymbolsByUDA ?enum Attr; struct A { Attr int a; int b; Attr void foo() {} Attr void foo(int) {} } void main() { import std.traits; import std.stdio; alias symbols = getSymbolsByUDA!(A, Attr); A a; static foreach (i; 0 .. symbols.length) { writeln("identifier: ", __traits(identifier, symbols[i])); static if (is(typeof(symbols[i]) == int)) { __traits(getMember, a, __traits(identifier, symbols[i])) = 42; } } assert(a.a == 42); }
Nov 24 2018
On Saturday, 24 November 2018 at 08:09:38 UTC, Stanislav Blinov wrote:On Saturday, 24 November 2018 at 03:48:12 UTC, Eko Wahyudin wrote:aah ya,, this statement is work static foreach (i; 0 .. symbols.length) thank you.Hi all, anyone know how to iterate getSymbolsByUDA ?enum Attr; struct A { Attr int a; int b; Attr void foo() {} Attr void foo(int) {} } void main() { import std.traits; import std.stdio; alias symbols = getSymbolsByUDA!(A, Attr); A a; static foreach (i; 0 .. symbols.length) { writeln("identifier: ", __traits(identifier, symbols[i])); static if (is(typeof(symbols[i]) == int)) { __traits(getMember, a, __traits(identifier, symbols[i])) = 42; } } assert(a.a == 42); }
Nov 24 2018
On Saturday, 24 November 2018 at 08:50:59 UTC, Eko Wahyudin wrote:On Saturday, 24 November 2018 at 08:09:38 UTC, Stanislav Blinov wrote:It should work with just: static foreach(sym; getSymbolsByUDA!(A, Attr)){ ... } or alias symbols = getSymbolsByUDA!(A, Attr); static foreach(sym; symbols){ ... } --- What you were missing was just making it a static foreach.On Saturday, 24 November 2018 at 03:48:12 UTC, Eko Wahyudin wrote:aah ya,, this statement is work static foreach (i; 0 .. symbols.length) thank you.Hi all, anyone know how to iterate getSymbolsByUDA ?enum Attr; struct A { Attr int a; int b; Attr void foo() {} Attr void foo(int) {} } void main() { import std.traits; import std.stdio; alias symbols = getSymbolsByUDA!(A, Attr); A a; static foreach (i; 0 .. symbols.length) { writeln("identifier: ", __traits(identifier, symbols[i])); static if (is(typeof(symbols[i]) == int)) { __traits(getMember, a, __traits(identifier, symbols[i])) = 42; } } assert(a.a == 42); }
Nov 24 2018
On Sunday, 25 November 2018 at 01:01:30 UTC, bauss wrote:On Saturday, 24 November 2018 at 08:50:59 UTC, Eko Wahyudin wrote:seems my problem is i use GDC, i got this error error: basic type expected, not foreach.On Saturday, 24 November 2018 at 08:09:38 UTC, Stanislav Blinov wrote:It should work with just: static foreach(sym; getSymbolsByUDA!(A, Attr)){ ... } or alias symbols = getSymbolsByUDA!(A, Attr); static foreach(sym; symbols){ ... } --- What you were missing was just making it a static foreach.[...]aah ya,, this statement is work static foreach (i; 0 .. symbols.length) thank you.
Nov 26 2018
On Monday, 26 November 2018 at 13:50:13 UTC, Eko Wahyudin wrote:seems my problem is i use GDC, i got this error error: basic type expected, not foreach.The common versions of gdc don't support static foreach, but plain foreach should work there the same way in this context. So just remove the word "static" and try it then.
Nov 26 2018
On 11/26/18 8:59 AM, Adam D. Ruppe wrote:On Monday, 26 November 2018 at 13:50:13 UTC, Eko Wahyudin wrote:Yeah, the original code used for, not foreach. for does not work on compile-time alias sequences (which is what getSymbolsByUDA evaluates to). Using foreach should work with most flavors of the compiler. static foreach was a recent addition to D, so I expect GDC will be getting support for it eventually. -Steveseems my problem is i use GDC, i got this error error: basic type expected, not foreach.The common versions of gdc don't support static foreach, but plain foreach should work there the same way in this context. So just remove the word "static" and try it then.
Nov 26 2018
static foreach(sym; getSymbolsByUDA!(A, Attr)){ writeln(sym.stringof); // print variable name "a" this what i want } foreach(sym; getSymbolsByUDA!(A, Attr)){ writeln(sym.stringof); // print "sym" } foreach(sym; getSymbolsByUDA!(A, Attr)){ writeln(__traits(identifier, sym)); //also print "sym" } is there a way to get variable name using foreach like static foreach?
Nov 27 2018
On 11/28/18 2:18 AM, Eko Wahyudin wrote:static foreach(sym; getSymbolsByUDA!(A, Attr)){ writeln(sym.stringof); // print variable name "a" this what i want } foreach(sym; getSymbolsByUDA!(A, Attr)){ writeln(sym.stringof); // print "sym" } foreach(sym; getSymbolsByUDA!(A, Attr)){ writeln(__traits(identifier, sym)); //also print "sym" } is there a way to get variable name using foreach like static foreach?Generally, foreach establishes a variable for the item, whereas static foreach does not. Note: I can't even get this to work, even in old versions of dmd. I get the Error: need `this` for `a` of type `int` But if you can get it to work, you may have luck with: alias symbols = getSymbolsByUDA!(A, Attr); foreach(i, _; symbols) { writeln(symbols[i].stringof); // or writeln(__traits(identifier, symbols[i])); } Which is an old workaround for how foreach on tuples used to work. -Steve
Nov 28 2018