[Issue 1610] New: Enum.stringof is int, not the name of the enum

Ary Manzana ary at esperanto.org.ar
Wed Oct 24 08:13:26 PDT 2007


d-bugmail at puremagic.com escribió:
> http://d.puremagic.com/issues/show_bug.cgi?id=1610
> 
>            Summary: Enum.stringof is int, not the name of the enum
>            Product: D
>            Version: 2.007
>           Platform: PC
>         OS/Version: Linux
>             Status: NEW
>           Severity: normal
>           Priority: P2
>          Component: DMD
>         AssignedTo: bugzilla at digitalmars.com
>         ReportedBy: andrei at metalanguage.com
> 
> 
> import std.stdio;
> enum ABC { a, b, c }
> void main() { writeln(ABC.stringof); } // prints "int"
> 
> This causes major issues in reflective and serialization code. The .stringof
> should evaluate to the name of the enum, e.g. "ABC".
> 
> 

lol, I just discovered the same thing yesterday.

I found out you can also do this:

void foo() {
    int x = typeof(ABC).a; // same as ABC.a
    int y = typeof(ABC).init; // y = 0
}

The compiler also allows me to do this:

enum ABCInit {
   a, b, c, init
}

Now if I do:

void foo() {
    int y = typeof(ABC).init; // y = 3
}

So an addition of a field "init" could potentialy and silently introduce 
bugs in the application. Although I think it's rare that someone would 
want to define an "init" property for an enum type (or for a class, or 
struct), I think the compiler should disallow this (same behaviour as 
trying to redefine the "sizeof", "alignof", etc., properties).


More information about the Digitalmars-d-bugs mailing list