Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
March 03, 2019 static foreach / iterate over all consts in a module? | ||||
---|---|---|---|---|
| ||||
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_") {
--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
|
March 04, 2019 Re: static foreach / iterate over all consts in a module? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert M. Münch | 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_") { Something like this? You can also use static foreach instead, but then you'll need to add another pair of `{}` brackets otherwise you'll get `member` is defined multiple times. https://run.dlang.io/is/7legpv module thisModule; struct MW_ {} const(MW_)* one; const(MW_)* two; void main() { import std.stdio; foreach(memberName ; __traits(allMembers, thisModule)) { alias member = __traits(getMember, thisModule, memberName); static if(is(typeof(member) : const(MW_)*)) { writeln(memberName, ": ", member); } } } |
March 04, 2019 Re: static foreach / iterate over all consts in a module? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert M. Münch | 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); } } } |
March 05, 2019 Re: static foreach / iterate over all consts in a module? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rubn | On 2019-03-04 00:37:56 +0000, Rubn said: > 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); > } > } > } Thanks. Trying this (I replaced thisModule by core.sys.windows.winuser everywhere) I get the following errors for the line with the "writeln": Error: unexpected ( in declarator Error: basic type expected, not ": " Error: found ": " when expecting ) Error: no identifier for declarator writeln(memberName, _error_) Error: semicolon expected following function declaration Error: declaration expected, not , Wondering why the "writeln" bombs out here... -- Robert M. Münch http://www.saphirion.com smarter | better | faster |
March 05, 2019 Re: static foreach / iterate over all consts in a module? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert M. Münch | 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_") { > static foreach(wm; __traits(allMembers, mymodule)) { > static if(wm.stringof[1..3] == "WM_") { |
March 05, 2019 Re: static foreach / iterate over all consts in a module? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert M. Münch | On 2019-03-05 12:57:15 +0000, Robert M. Münch said: > On 2019-03-04 00:37:56 +0000, Rubn said: > >> 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); >> } >> } >> } > > Thanks. > > Trying this (I replaced thisModule by core.sys.windows.winuser everywhere) I get the following errors for the line with the "writeln": > > Error: unexpected ( in declarator > Error: basic type expected, not ": " > Error: found ": " when expecting ) > Error: no identifier for declarator writeln(memberName, _error_) > Error: semicolon expected following function declaration > Error: declaration expected, not , > > Wondering why the "writeln" bombs out here... I had this CTFE loop in the global scope, I think it's related. Using pragma(msg, ...) works in the global scope. -- Robert M. Münch http://www.saphirion.com smarter | better | faster |
Copyright © 1999-2021 by the D Language Foundation