www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4261] New: Bad textual printing of enums

reply d-bugmail puremagic.com writes:
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



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: -------
Jun 02 2010
next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Aug 29 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4261




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: -------
Aug 30 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4261




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: -------
Aug 30 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4261




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: -------
Aug 31 2010
prev sibling parent d-bugmail puremagic.com writes:
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





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: -------
Jun 02 2011