Thread overview
[Issue 4261] New: Bad textual printing of enums
Aug 30, 2010
Andrej Mitrovic
Aug 30, 2010
Andrej Mitrovic
Jun 02, 2011
kennytm@gmail.com
June 02, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4261

           Summary: Bad textual printing of enums
           Product: D
           Version: unspecified
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-06-02 14:47:27 PDT ---
This D2 program:

import std.stdio: writeln;
void main() {
    enum Foo { Zero, One }
    Foo f = Foo.One;
    writeln(f);
}


With DMD v2.046 prints:
1

But it's better for it to print:
One


See also bug 3308.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 30, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4261


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2010-08-29 21:16:37 PDT ---
When you need to get the name of the enumerated value, you have to use the 'to' template function from std.conv, as described in TDPL. Your example then becomes:

import std.conv : to;
import std.stdio: writeln;
void main()
{
    enum Foo { Zero, One }
    Foo f = Foo.One;
    writeln(to!string(f));
}

Prints: One

It wouldn't make much sense for an enum to behave differently in different contexts (e.g. comparing it in an if statement vs. using it with a writeln).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 30, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4261



--- Comment #2 from bearophile_hugs@eml.cc 2010-08-30 04:37:55 PDT ---
Enums aren't numbers, they are symbols that the language/CPU often represents with numbers.

See also bug 3999

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 30, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4261



--- Comment #3 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2010-08-30 05:47:34 PDT ---
I'm not sure I understand what you're saying. An enum in D is a list of symbolic values of any type (except classes).

For example:

module enums;

import std.stdio;

struct Color
{
    ubyte r, g, b;
}

enum { red = Color(255, 0, 0), green = Color(0, 255, 0), blue = Color(0, 0,
255) }

void foo(Color c)
{
    writefln("%s %s %s", c.r, c.g, c.b);
}

void main()
{
    foo(blue);
}

enums are also used in CTFE, e.g.:

enum float value = someFunc();  // CTFE

Having to use a cast everywhere for an enum would break a lot of code.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 01, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4261



--- Comment #4 from bearophile_hugs@eml.cc 2010-08-31 18:57:48 PDT ---
What I am saying in bug 3999 is relative to just the case where the enum has a EnumTag. In this case I prefer the enum to be like a typedef (as the C++0x "enum class") and require a cast if you want to use/compare it as/to the base type.

In your example red, green and blue are inside an anonymous enum (it lacks a EnumTag), so in this case the cast is not necessary. So that code is not affected.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 02, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4261


kennytm@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |kennytm@gmail.com
         Resolution|                            |FIXED


--- Comment #5 from kennytm@gmail.com 2011-06-02 11:13:54 PDT ---
Recently fixed by Phobos pull #65.

https://github.com/D-Programming-Language/phobos/commit/cbe0d06965db56599f9b35e7e2a131a99dbd9ed2

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------