Thread overview
%s not producing string representation of enum?
Dec 10, 2015
Shriramana Sharma
Dec 10, 2015
Daniel Kozak
Dec 10, 2015
Daniel Kozak
Dec 10, 2015
Basile B.
Dec 10, 2015
Basile B.
Dec 11, 2015
Shriramana Sharma
Dec 11, 2015
Ali Çehreli
Dec 11, 2015
Shriramana Sharma
December 10, 2015
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
December 10, 2015
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));
}

December 10, 2015
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) { ... }

December 10, 2015
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 ;) ).
December 10, 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);
    }
}
~~~~~~~~~~~~

December 11, 2015
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
December 11, 2015
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

December 11, 2015
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