Implicit enum conversions are a stupid PITA

Nick Sabalausky a at a.a
Tue Mar 23 16:14:01 PDT 2010


"yigal chripun" <yigal100 at gmail.com> wrote in message 
news:hobg4b$12ej$1 at digitalmars.com...
>
> This also interacts with the crude hack of "this enum is actually a 
> constant".
> if you remove the implicit casts than how would you be able to do:
> void foo(int p);
> enum { bar = 4 }; // don't remember the exact syntax here
> foo(bar); // compile-error?!
>

AIUI, That style enum is already considered different by the compiler 
anyway. Specifically, it's doesn't create any new type, whereas the other 
type of enum creates a new semi-weak type. I don't think it would be too big 
of a step to go one step further and change "this kind of enum creates a new 
semi-weak type" to "this kind of enum creates a new strong type". But yea, I 
absolutely agree that calling a manifest constant an "enum" is absurd. It 
still bugs the hell out of me even today, but I've largely shut up about it 
since Walter hasn't wanted to change it even though he seems to be the only 
one who doesn't feel it's a bad idea (and it's not like it causes practical 
problems when actually using the language...although I'm sure it must be a 
big WTF for new and prospective D users).


> I feel that enum needs to be re-designed. I think that C style "enums are 
> numbers" are *bad*, *wrong* designs that expose internal implementation 
> and the only valid design is that of Java 5.
>
> e.g.
> enum Color {blue, green}
> Color c = Color.blue;
> c++; // WTF?  should NOT compile
>
> A C style enum with values assigned is *not* an enumeration but rather a 
> set of meaningful integral values and should be represented as such.
>
> This was brought up many many times in the NG before and based on past 
> occurences will most likely never change.

I would hate to see enums lose the concept of *having* a base type and base 
values because I do find that to be extremely useful (Haxe's enums don't 
have a base type and, from direct experience with them, I've found that to 
be a PITA too). But I feel very strongly that conversions both to and from 
the base type need to be explicit. In fact, that was one of the things that 
was bugging me about C/C++ even before I came across D. D improves the 
situation of course, but it's still only half-way.






More information about the Digitalmars-d mailing list