digitalmars.D.learn - It's possible to declare a variable inside a static foreach()?
- Marc (7/15) Dec 21 2017 For example, I'd like to declare a variable inside a static
- Anonymouse (7/23) Dec 21 2017 I don't think you need static for foreach of __traits
- Seb (9/34) Dec 21 2017 The trick with static foreach is to create a new scope:
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?
Dec 21 2017
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.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/Ln3kVZenum 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?/*static*/ foreach(C c; attributes)Mind that c will not be of type C but of the type of the attribute.
Dec 21 2017
On Thursday, 21 December 2017 at 16:38:36 UTC, Anonymouse wrote:On Thursday, 21 December 2017 at 16:25:00 UTC, Marc wrote: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/5729For 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.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/Ln3kVZenum 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?/*static*/ foreach(C c; attributes)Mind that c will not be of type C but of the type of the attribute.
Dec 21 2017