Making enum join variadic

"Nordlöw" via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 2 06:38:38 PDT 2014


On Friday, 2 May 2014 at 12:45:44 UTC, Nordlöw wrote:
>> I'd be verbose. It's an uncommon operation, bound to surprise 
>> a reader
>> a bit.It's better to type a few more letters.
>
> See update.
>
>> I did not try to compile it, but what happens if the enum 
>> elements
>> have the same name ? The same min/max/values ?
>>
>> Like this:
>>
>> enum E1 { a, b, c }
>>
>> alias E111 = join!(E1, E1, E1);
>
> This should give a better error message, than current mixin 
> error.
> I'll work on that.
>
>> I would put it in std.typecons, since it's a type constructor
>
> Ok.
>
> Thx!

Here's my try at detecting member names collision at compile time:

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?


More information about the Digitalmars-d-learn mailing list