Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
November 24, 2018 How to iterate getSymbolsByUDA | ||||
---|---|---|---|---|
| ||||
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 |
November 24, 2018 Re: How to iterate getSymbolsByUDA | ||||
---|---|---|---|---|
| ||||
Posted in reply to Eko Wahyudin | 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);
}
|
November 24, 2018 Re: How to iterate getSymbolsByUDA | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | 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.
|
November 25, 2018 Re: How to iterate getSymbolsByUDA | ||||
---|---|---|---|---|
| ||||
Posted in reply to Eko Wahyudin | 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.
|
November 26, 2018 Re: How to iterate getSymbolsByUDA | ||||
---|---|---|---|---|
| ||||
Posted in reply to bauss | 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.
|
November 26, 2018 Re: How to iterate getSymbolsByUDA | ||||
---|---|---|---|---|
| ||||
Posted in reply to Eko Wahyudin | 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.
|
November 26, 2018 Re: How to iterate getSymbolsByUDA | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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
|
November 28, 2018 Re: How to iterate getSymbolsByUDA | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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? |
November 28, 2018 Re: How to iterate getSymbolsByUDA | ||||
---|---|---|---|---|
| ||||
Posted in reply to Eko Wahyudin | 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
|
Copyright © 1999-2021 by the D Language Foundation