Feature request: enum init shouldn't create a new enumeration

Jonathan M Davis jmdavisProg at gmx.com
Sat Oct 13 23:51:17 PDT 2012


On Sunday, October 14, 2012 08:20:40 Tommi wrote:
> There's a bug in that code, because MyEnum default-initializes to
> an invalid value. It's effectively the same as this following
> code, where the programmer has failed to initialize MyEnum
> variable with a valid value:

Valid or not, MyEnum.init is still a value for MyEnum and _must_ be accounted 
for. Trying to treat MyEnum.init as not being a value of MyEnum is likely to 
break all kinds of stuff. Being able to have a variable of an enum type with a 
value which is not really a member of the enum is just plain broken.

And honestly, declaring a specific init value for an enum is a stupid idea. 
It's going to screw with all kinds of stuff. Anything assuming that the init 
property is the first value (is it is in _all_ other cases but isn't 
necessarily if you declare your own) will be broken. It's still going to end 
up being in stuff like std.traits.EnumMembers or pretty much anything which 
operates on enums unless all kinds of special casing is added. TDPL does 
mention (p. 275) that you can declare enum members with the names max, min, 
and init, but it also points out that it's a dumb idea. I'd argue that it 
shouldn't even be legal at all. It's just begging for trouble.

- Jonathan M Davis


More information about the Digitalmars-d mailing list