%s not producing string representation of enum?

Basile B. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Dec 10 10:46:14 PST 2015


On Thursday, 10 December 2015 at 14:46:26 UTC, Basile B. wrote:
> On Thursday, 10 December 2015 at 14:24:43 UTC, Shriramana 
> Sharma wrote:
>> Hello. I'm using DMD 2.069.2. As per 
>> http://ddili.org/ders/d.en/enum.html the following code is 
>> supposed to output the *names* of the suits:
>>
>> import std.stdio;
>> void main()
>> {
>>     enum Suit { spades, hearts, diamonds, clubs }
>>     foreach (suit; Suit.min .. Suit.max + 1) { writefln("%s", 
>> suit); }
>> }
>>
>> But I'm getting 0 1 2 3. Kindly advise.
>
> You should rather use std.traits.EnumMembers to iterate the

By the way I should use format() in the example, but writeln() 
use the same methods:

~~~~~~~~~~~~
import std.stdio;

void main(string[] args)
{
     import std.traits: EnumMembers;
     enum Suit { spades = 23.2, hearts = 8.3, diamonds = 12.56, 
clubs = 1.3 }
     foreach(i,member; EnumMembers!Suit)
     {
         writefln("%s", member);
         writefln("%f", member);
         writeln(i);
     }
}
~~~~~~~~~~~~



More information about the Digitalmars-d-learn mailing list