C#'s greatest mistakes

Stewart Gordon smjg_1998 at yahoo.com
Sun Nov 28 18:09:43 PST 2010


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....

Stewart.


More information about the Digitalmars-d mailing list