enum

Jonathan M Davis jmdavisProg at gmx.com
Thu Apr 10 23:25:49 PDT 2014


On Thursday, April 10, 2014 22:40:14 Walter Bright wrote:
> On 4/10/2014 10:29 PM, Jonathan M Davis wrote:
> > If you just want to give a list of values names, then you can use manifest
> > constants rather than an enumeration.
> 
> You could, but then you'd lose the self-documenting aspect of static type
> checking. See the "Color" example.
> 
>      alias int Color;
> 
> doesn't offer any protection from a Color value getting mixed up with a file
> mode mask.

Except that the only static type checking you're getting is protection against 
direct assignment. It's trivial to mutate an existing Color to a value not 
enumerated in Color. Also, what's the point of protecting the type if the 
intention is that the enumeration only lists _some_ of the values? If it's 
only listing some of the values, then it's expected that other values could 
and will be used, so the protection against assigning other values to Color 
would in fact be counterproductive.

And if an enumeration _is_ listing all of the values, then why not fully 
protect the enum type and make it illegal to do any operation on it (other 
than a cast) which makes it be a value other than one of the enumerated 
values?

Really, as it stands, enum types provides next to no protection against having 
variables of that type be values other than the enumerated ones

Either enumerations only list some of the expected values, in which case, the 
protection against assignment of other values is a hindrance when unlisted 
values are used, or enumerations list all of the values, in which case, 
protecting only direct assignment isn't enough.

- Jonathan M Davis


More information about the Digitalmars-d mailing list