Template mixin problem with EnumMembers

Ferhat Kurtulmuş aferust at gmail.com
Sat Oct 2 22:07:23 UTC 2021


The below code works as expected on https://run.dlang.io/, but 
not on my computer. And I don't know why?

I want to access enum members like Options.OUT_FILE_NAME instead 
of Options.StringOption.OUT_FILE_NAME.

Please note that the solutions like "alias something = int;" 
don't work for me. that is not what I need.

In my local computer, pragma(msg, fullname) outputs like:
cast(StringOption)0
cast(StringOption)1
cast(StringOption)2

some error messages I get:
...\options.d-mixin-86(86,11): Error: declaration expected, not )
...\options.d-mixin-86(86,11): Error: no identifier for 
declarator tion

```d
import std.stdio;

class Options{
     public:

     enum StringOption {
         OUT_FILE_NAME,
         RPT_FILE_NAME,
         MAP_FILE_NAME
     }

     import std.traits: EnumMembers;
     mixin template StrOptAliases()
     {
         static foreach(fullname; [EnumMembers!(StringOption)]){
             mixin("alias " ~ fullname.stringof[13..$] ~ " = " ~ 
"Options." ~
                                                    
fullname.stringof ~ ";\n");
             pragma(msg, fullname);
         }
     }

     mixin StrOptAliases;
}

void main()
{
     int opt = Options.OUT_FILE_NAME;
     writeln(opt);
}
```


More information about the Digitalmars-d-learn mailing list