digitalmars.D - static foreach / iterate over all consts in a module?
- =?iso-8859-1?Q?Robert_M._M=FCnch?= (10/10) Mar 03 2019 If I want to iterate over all const WM_* windows message definitions
- Rubn (21/27) Mar 03 2019 Something like this? You can also use static foreach instead, but
- Rubn (23/29) Mar 03 2019 Ah I misunderstood, thought that * was a pointer not a wildcard
- =?iso-8859-1?Q?Robert_M._M=FCnch?= (15/38) Mar 05 2019 Thanks.
- =?iso-8859-1?Q?Robert_M._M=FCnch?= (7/46) Mar 05 2019 I had this CTFE loop in the global scope, I think it's related. Using
- Nicholas Wilson (1/9) Mar 05 2019
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
Mar 03 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_") {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); } } }
Mar 03 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); } } }
Mar 03 2019
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
Mar 05 2019
On 2019-03-05 12:57:15 +0000, Robert M. Münch said:On 2019-03-04 00:37:56 +0000, Rubn said: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 | fasterIt 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...
Mar 05 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_") {static foreach(wm; __traits(allMembers, mymodule)) { static if(wm.stringof[1..3] == "WM_") {
Mar 05 2019