Using getSymbolsByUDA in a static foreach loop

Jack Stouffer jack at jackstouffer.com
Thu Jan 20 01:26:08 UTC 2022


On Thursday, 20 January 2022 at 01:14:51 UTC, Adam Ruppe wrote:
> On Thursday, 20 January 2022 at 00:55:33 UTC, Jack Stouffer 
> wrote:
>>             static foreach(member; __traits(allMembers, 
>> Manager))
>
> member here is a string, not the member. I prefer to call it 
> memberName.
>
> Then you __traits(getMember, Manager, memberName) to actually 
> get the alias you can pass to getAttributes.

Thanks, that fixed it. Final working version for anyone who finds 
this thread:

```d
import std.traits;
import std.stdio;

enum Runnable;

struct SubSystem
{
     void run()
     {
     	writeln("SubSystem ran");
     }
}

struct Manager
{
     @Runnable SubSystem subsystem;

     void run()
     {
         static foreach(memberName; __traits(allMembers, Manager))
         {
             static foreach (attribute; __traits(getAttributes, 
__traits(getMember, Manager, memberName)))
             {
                 static if (is(attribute == Runnable))
                 {
                     __traits(getMember, Manager, 
memberName).run();
                 }
             }
         }
     }
}


void main()
{
     Manager m;
     m.run();
}
```


More information about the Digitalmars-d-learn mailing list