EnumBaseType conversion

dsimcha dsimcha at yahoo.com
Tue Jun 16 07:47:57 PDT 2009


I was actually about to file this as a bug until I double checked the spec and
realized that, as ridiculous as it seems, this is correct according to the spec.

What is the rationale for allowing named enums to implicitly convert to their
EnumBaseType?  This seems like an unnecessarily dangerous feature to me (named
enums are supposed to create a distinct type, and you can always explicitly
cast them).  It has bitten me several times when I have a named enum type next
to an integer type or something that an integer can be implicitly converted to
in a function param list:

enum MyEnum {
    FOO,
    BAR
}

void doStuff(MyEnum me, int i) {}
void doRealStuff(MyEnum me, real i) {}

void main() {
    doStuff(MyEnum.BAR, MyEnum.FOO);
    doRealStuff(MyEnum.BAR, MyEnum.FOO);
}

IMHO if you want manifest constant behavior that does not create a distinct
type, that's what anonymous enums are for.  If you just want a namespace and
don't want to create a new type, you can just use a dummy struct:

struct MyEnum {
    enum FOO = 0;
    enum BAR = 1;
}



More information about the Digitalmars-d mailing list