Enum literals, good? bad? what do you think?

Mike Parker aldacron at gmail.com
Wed Jul 21 07:12:25 UTC 2021


On Wednesday, 21 July 2021 at 06:39:18 UTC, Walter Bright wrote:

>
> But if you still want unqualified names, `alias` is the feature:
>
>
>     enum MyEnum { blah, bleh, bluh }
>     alias blah = MyEnum.blah;
>     alias bleh = MyEnum.bleh;
>     alias bluh = MyEnum.bluh;
>
> `alias` is an all-purpose tool for moving names from one scope 
> to another. Of course, one can probably do a mixin to automate 
> the alias declarations. It should be a fun exercise. Any takers?
>
> (As mentioned by others, `with` does it too, but `with` only 
> affects the scope it specifies.)

```d
enum expandEnum(EnumType, string fqnEnumType = EnumType.stringof) 
= (){
     string expandEnum = "enum {";
     foreach(m;__traits(allMembers, EnumType)) {
         expandEnum ~= m ~ " = " ~ fqnEnumType ~ "." ~ m ~ ",";
     }
     expandEnum  ~= "}";
     return expandEnum;
}();

enum MyEnum { blah, bleh, bluh }
mixin(expandEnum!MyEnum);

auto e = blah;
```


More information about the Digitalmars-d mailing list