www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - %s not producing string representation of enum?

reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
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.

-- 
Shriramana Sharma, Penguin #395953
Dec 10 2015
next sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
V Thu, 10 Dec 2015 19:54:43 +0530
Shriramana Sharma via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> napsáno:

 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.
 
void main() { writeln(typeid(typeof(Suit.max))); writeln(typeid(typeof(Suit.max + 1))); } + 1 makes int from Suit import std.stdio; import std.conv; foreach (suit; Suit.min .. Suit.max + 1) { enum Suit { spades, hearts, diamonds, clubs } writefln("%s", to!Suit(suit)); }
Dec 10 2015
prev sibling next sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
V Thu, 10 Dec 2015 19:54:43 +0530
Shriramana Sharma via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> napsáno:

 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.
 
or: foreach (suit; Suit.min .. Suit.max + cast(Suit)1) { ... }
Dec 10 2015
prev sibling parent reply Basile B. <b2.temp gmx.com> writes:
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 members of an enum: ~~~~~~~~~~~~ import std.stdio; void main(string[] args) { import std.traits: EnumMembers; enum Suit { spades, hearts, diamonds, clubs } foreach(member; EnumMembers!Suit) writeln(member); } ~~~~~~~~~~~~ it can also counts ~~~~~~~~~~~~ import std.stdio; void main(string[] args) { import std.traits: EnumMembers; enum Suit { spades = 23.2, hearts, diamonds, clubs } foreach(i,member; EnumMembers!Suit) { writeln(member); writeln(i); } } ~~~~~~~~~~~~ which is interesting to get the rank of a member, considering that the rank is not always the same as the value (like here, it's a float ;) ).
Dec 10 2015
next sibling parent Basile B. <b2.temp gmx.com> writes:
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); } } ~~~~~~~~~~~~
Dec 10 2015
prev sibling parent reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
Basile B.  wrote:

 You should rather use std.traits.EnumMembers to iterate the
 members of an enum:
Wow this is great! I was thinking that enum.max + 1 is not really befitting D's elegant approach to programming. Ali should really update that section of his book to use EnumMembers. This will both help avoid the above hack and only then will his reference to %s and %d having different effects for enums be true... Thanks once more to Ali for his book and you people for the replies! -- Shriramana Sharma, Penguin #395953
Dec 10 2015
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/10/2015 08:58 PM, Shriramana Sharma wrote:

 Ali should really update that section
 of his book to use EnumMembers.
Done[1]: http://ddili.org/ders/d.en/enum.html#ix_enum.EnumMembers,%20std.traits Thank you, Ali [1] https://bitbucket.org/acehreli/ddili/commits/5f146bdc921c6e82c10a107906bd45fa325b0e82#chg-src/ders/d.en/preface.d
Dec 11 2015
parent Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
Ali Çehreli wrote:

 http://ddili.org/ders/d.en/enum.html#ix_enum.EnumMembers,%20std.traits
Ali that's great! Thank you! -- Shriramana Sharma, Penguin #395953
Dec 11 2015