www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to iterate getSymbolsByUDA

reply Eko Wahyudin <eko.wahyudin yahoo.co.id> writes:
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
parent reply Stanislav Blinov <stanislav.blinov gmail.com> writes:
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
parent reply Eko Wahyudin <eko.wahyudin yahoo.co.id> writes:
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:
 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); }
aah ya,, this statement is work static foreach (i; 0 .. symbols.length) thank you.
Nov 24 2018
parent reply bauss <jj_1337 live.dk> writes:
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:
 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); }
aah ya,, this statement is work static foreach (i; 0 .. symbols.length) thank you.
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.
Nov 24 2018
parent reply Eko Wahyudin <eko.wahyudin yahoo.co.id> writes:
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:
 On Saturday, 24 November 2018 at 08:09:38 UTC, Stanislav 
 Blinov wrote:
 [...]
aah ya,, this statement is work static foreach (i; 0 .. symbols.length) thank you.
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.
seems my problem is i use GDC, i got this error error: basic type expected, not foreach.
Nov 26 2018
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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
next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 11/26/18 8:59 AM, Adam D. Ruppe wrote:
 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.
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. -Steve
Nov 26 2018
prev sibling parent reply Eko Wahyudin <eko.wahyudin yahoo.co.id> writes:
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
parent Steven Schveighoffer <schveiguy gmail.com> writes:
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