It's possible to declare a variable inside a static foreach()?
Anonymouse
asdf at asdf.net
Thu Dec 21 16:38:36 UTC 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.
>
>> 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.
More information about the Digitalmars-d-learn
mailing list