static foreach / iterate over all consts in a module?

Rubn where at is.this
Mon Mar 4 00:37:56 UTC 2019


On Sunday, 3 March 2019 at 19:20:00 UTC, Robert M. Münch wrote:
> If I want to iterate over all const WM_* windows message 
> definitions during compile time, can this be done with a static 
> foreach and the allMembers traits?
>
> Something like:
>
> static foreach(wm; __traits(allMembers, mymodule)) {
> 	static if(wm[1..3] == "WM_") {

Ah I misunderstood, thought that * was a pointer not a wildcard 
:P.

It depends on how it is stored, if it is just an enum, then yes 
you can do pretty much what you have already. Some example of 
what you have already would help as well.

https://run.dlang.io/is/l2jyWP

module thisModule;

enum WM_CREATE = 1;
enum WM_DESTROY = 2;

void main()
{
     import std.stdio;

     foreach(memberName ; __traits(allMembers, thisModule))
     {
         alias member = __traits(getMember, thisModule, 
memberName);
         static if(memberName.length >= 3 && memberName[0 .. 3] == 
"WM_") {
             writeln(memberName, ": ", member);
         }
     }
}


More information about the Digitalmars-d mailing list