How to iterate getSymbolsByUDA
bauss
jj_1337 at live.dk
Sun Nov 25 01:01:30 UTC 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:
>> 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.
More information about the Digitalmars-d-learn
mailing list