foreach over string enum
Jesse Phillips
jessekphillips+D at gmail.com
Thu Feb 10 14:32:21 PST 2011
spir Wrote:
> On 02/10/2011 08:22 PM, Jesse Phillips wrote:
> > enum FileName : string {
> > file1 = "file1.ext",
> > file2 = "file2.ext"
> > }
> >
> > void main(string args[])
> > {
> > foreach(a; __traits(allMembers, FileName))
> > writeln(mixin("FileName." ~ a));
> > }
>
> Why the mixin? Is it (just) to have the output string computed at compile-time?
The value of 'a' is the enum field name, not its value. The code will ultimately expand to:
void main() {
writeln(FileName.file1));
writeln(FileName.file2));
}
Which in turn will may have its enum contents expanded to the string value. The mixin is required because it is a named enum and 'a' is a string, not an alias:
writeln(FileName.a)
doesn't work and nor does:
writeln(FileName.mixin(a));
More information about the Digitalmars-d-learn
mailing list