C#'s greatest mistakes

Jonathan M Davis jmdavisProg at gmx.com
Sun Nov 28 20:51:12 PST 2010


On Sunday 28 November 2010 18:09:43 Stewart Gordon wrote:
> On 27/11/2010 03:27, Andrei Alexandrescu wrote:
> > http://oredev.org/2010/sessions/c-s-greatest-mistakes
> > 
> > Andrei
> 
> Looking at the bit about enums....
> 
> Java enums and C, D, etc. enums have their pros and cons.  I suppose the
> main advantages of C enums are:
> - lightweight
> - can also be used as bit sets
> 
> You could argue that bit sets ought to be a separate type.  But this
> precludes uses that combine "proper" enum use with a bit flag or two.
> Take, from one of my programs,
> 
> enum Piece : byte {
>      EMPTY, PAWN, BISHOP, KNIGHT,
>      ROOK_CANNOT_CASTLE, ROOK_CAN_CASTLE, QUEEN, KING,
>      BLACK = 8,
>      LAST = Piece.BLACK | KING
> }
> 
> The first 8 values are mutually exclusive, BLACK is a bit flag, and LAST
> is just a marker value.  But the compiler doesn't stop you doing silly
> stuff like Piece.PAWN | Piece.BISHOP.  Maybe someone can come up with a
> better way of doing this....
> 
> Java enums preclude having an array indexed by them.  I suppose the
> ability to store data in the enum objects was meant to make up for this.
>   But I recall that somebody in one company I briefly worked for didn't
> really like Java enums because of this complexity....

D has the best of both worlds (or at least will, once bug 
http://d.puremagic.com/issues/show_bug.cgi?id=4423 has been fixed). You can have 
enums of primitive types such as int, or you can use structs for more complex 
types like you'd get in Java. So, you can make your enum whatever type is most 
appropriate for what you're doing with it.

- Jonathan M Davis


More information about the Digitalmars-d mailing list