enum

Jonathan M Davis jmdavisProg at gmx.com
Fri Apr 11 12:41:52 PDT 2014


On Friday, April 11, 2014 11:32:36 Walter Bright wrote:
> On 4/11/2014 4:18 AM, Jonathan M Davis wrote:
> > I don't see much point to enums if they're not intended to list
> > all of their values.
> 
> Again, bit masks, Color, etc., and to provide a simple integral type that
> behaves like an integral type yet can be overloaded and type checked.
> 
> Andrei has pointed out that these uses are unsound if you desire that the
> enumeration lists all possible values, and he's right. But I don't think
> that automatically makes them pointless.

IMHO, it's pointless to then declare variables of that enum type. Being able 
to have something like

enum Color : uint { red = 0xFF0000, green = 0x00FF00, blue = 0x0000FF }

is useful for listing constants, but it then doesn't make any sense IMHO to 
declare any variables of type Color. It's purpose is to group constants 
together, not really to declare a new type. If you want functions to have an 
integral type representing color where not all the values are enumerated, why 
not just use an alias? The only differences are protecting against 
initializing or assigning an enum variable to other values (which would just 
be annoying with other values, not helpful) and function overloading. And it 
just seems bug-prone to me to try and overload on an enum type when it's just 
a list of constants, and it's expected that other, unlisted values will be 
used (especially when one of the other overloads is for the base type). If you 
miss one cast, it'll call the wrong overload.

So, while I agree that having a listing of associated constants can be useful, 
I completely disagree that it makes any sense for declaring a list of 
associated constants to then result in a new type.

- Jonathan M Davis


More information about the Digitalmars-d mailing list