It's possible to declare a variable inside a static foreach()?
For example, I'd like to declare a variable inside a static foreach like in below code, just for better organization, otherwise, I have to use the value directly instead of the variable. If the value is used more than once, it might be inviable. enum allMembers = __traits(derivedMembers, C); static foreach(enum string member; allMembers) { enum attributes = __traits(getAttributes, __traits(getMember, C, member)); static foreach(C c; attributes) { writeln(c); } } I got redefinition erros of "atributes" on this. Can I have this only at compile time?
Re: It's possible to declare a variable inside a static foreach()?
On Thursday, 21 December 2017 at 16:25:00 UTC, Marc wrote: For example, I'd like to declare a variable inside a static foreach like in below code, just for better organization, otherwise, I have to use the value directly instead of the variable. If the value is used more than once, it might be inviable. enum allMembers = __traits(derivedMembers, C); static foreach(enum string member; allMembers) { enum attributes = __traits(getAttributes, __traits(getMember, C, member)); static foreach(C c; attributes) { writeln(c); } } I got redefinition erros of "atributes" on this. Can I have this only at compile time? I don't think you need static for foreach of __traits allMembers/derivedMembers and .tupleof. It unrolls at compile-time and builds fine for me if I just remove the statics. https://run.dlang.io/is/Ln3kVZ /*static*/ foreach(C c; attributes) Mind that c will not be of type C but of the type of the attribute.
Re: It's possible to declare a variable inside a static foreach()?
On Thursday, 21 December 2017 at 16:38:36 UTC, Anonymouse wrote: On Thursday, 21 December 2017 at 16:25:00 UTC, Marc wrote: For example, I'd like to declare a variable inside a static foreach like in below code, just for better organization, otherwise, I have to use the value directly instead of the variable. If the value is used more than once, it might be inviable. enum allMembers = __traits(derivedMembers, C); static foreach(enum string member; allMembers) { enum attributes = __traits(getAttributes, __traits(getMember, C, member)); static foreach(C c; attributes) { writeln(c); } } I got redefinition erros of "atributes" on this. Can I have this only at compile time? I don't think you need static for foreach of __traits allMembers/derivedMembers and .tupleof. It unrolls at compile-time and builds fine for me if I just remove the statics. https://run.dlang.io/is/Ln3kVZ /*static*/ foreach(C c; attributes) Mind that c will not be of type C but of the type of the attribute. The trick with static foreach is to create a new scope: https://run.dlang.io/is/wODA0x DIP1010 [1] suggested __local but while this is implemented, it it's not exposed yet. There's also a bit of discussion about this at [2]. [1] https://github.com/dlang/DIPs/blob/master/DIPs/DIP1010.md#local-declarations [2] https://github.com/dlang/phobos/pull/5729