DIPX: Enum Literals / Implicit Selector Expression

ryuukk_ ryuukk.dev at gmail.com
Thu Jun 30 11:26:01 UTC 2022


On Thursday, 30 June 2022 at 11:22:40 UTC, Mike Parker wrote:
> On Thursday, 30 June 2022 at 11:06:31 UTC, The Zealot wrote:
>
>>
>> you can always alias the enums into global scope too, if you 
>> want to have short names.
>>
>
> E.g.,
>
> ```D
> enum expandEnum(EnumType, string fqnEnumType = 
> EnumType.stringof) = (){
>     string expandEnum;
>     foreach(m;__traits(allMembers, EnumType)) {
>         expandEnum ~= "alias " ~ m ~ " = " ~ fqnEnumType ~ "." 
> ~ m ~ ";";
>     }
>     return expandEnum;
> }();
>
> enum Color {
>     red,
>     green,
>     blue,
>     yellow,
>     orange,
>     black,
>     white,
> }
> mixin(expandEnum!Color);
>
> void main() {
>     auto c = green;
>     writeln(c);
> }
> ```

This is exactly why we need it to be implemented, to avoid 
putting things in the global scope and therefore leaking

It better to make use of the type system and be a little more 
cleaner without having to rely on mixin/template/imports

It should just work


More information about the Digitalmars-d mailing list