foreach over string enum

spir denis.spir at gmail.com
Thu Feb 10 13:45:22 PST 2011


On 02/10/2011 08:22 PM, Jesse Phillips wrote:
> Nrgyzer Wrote:
>
>> Hey guys,
>>
>> I'm trying to iterate over an enumeration which contains strings like
>> the this:
>>
>> enum FileName : string {
>> file1 = "file1.ext",
>> file2 = "file2.ext"
>> }
>>
>> I already found this article: http://lists.puremagic.com/pipermail/
>> digitalmars-d/2007-July/021920.html but it's an enum which contains
>> integers instead of strings, thus I can't use min- or max-property.
>>
>> Any suggestions ->  thanks in advance!
>
> I'll just be leaving this here, if you need more explanation ask, but maybe you'll understand:
>
> import std.stdio;
>
> 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?

Denis
-- 
_________________
vita es estrany
spir.wikidot.com



More information about the Digitalmars-d-learn mailing list