Making enum join variadic

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 2 08:27:12 PDT 2014


On Friday, 2 May 2014 at 15:18:06 UTC, Artur Skawina via 
Digitalmars-d-learn wrote:
> On 05/02/14 15:38, "Nordlöw" via Digitalmars-d-learn wrote:
>> 
>> template MemberNamesUnion(E...) if (allSatisfy!(isEnum, E))
>> {
>>     bool[string] allMembers;   // used to detect member 
>> collisions
>>     mixin({
>>             string r = "enum MemberNamesUnion { ";
>>             foreach (T; E) {
>>                 import std.range: join;
>>                 foreach (member; __traits(allMembers, T)) {
>>                     static assert (member in allMembers, 
>> "Member collision");
>>                     allMembers[member] = true;
>>                 }
>>                 r ~= [__traits(allMembers, T)].join(",") ~ ",";
>>             }
>>             return r ~ " }";
>>         }());
>> }
>> 
>> It fails as
>> 
>> enums.d(25,46): Error: static variable allMembers cannot be 
>> read at compile time
>> enums.d(25,21):        while evaluating: static assert("a" in 
>> allMembers)
>> 
>> Is there a solution to this problem?
>
> Move the AA declaration to inside the lambda, remove the 
> 'static'
> from the assert, and fix the condition ("member !in 
> allMembers"), then
> it will work.
>
> artur

But that will also move the compile time check to a runtime one.


More information about the Digitalmars-d-learn mailing list