|
Archives
D Programming
digitalmars.Ddigitalmars.D.bugs digitalmars.D.dtl digitalmars.D.ide digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger D.gnu D C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript electronics |
digitalmars.D - EnumBaseType conversion
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;
}
Jun 16 2009
There's a simple solution: don't use that failure of a language
construct called enum, and write your own one. You simply can do
something Java-style, where enums basically are just normal classes
generated by the compiler frontend. In D, you can use structs, and
string mixins can generate the appropriate fields. Something like:
struct YourEnum(char[] fields) {
private int value;
mixin(generatefields());
static char[] generatefields() { return YOURMAGICHERE(fields); }
}
Enum fields are static and of the type YourEnum. e.g.
YourEnum("FOO,BAR") would add these members to the struct:
const YourEnum FOO = YourEnum(0);
const YourEnum BAR = YourEnum(1);
These are generated by the CTFE function generatefields().
As long as the value field is hidden, you have enums that are even more
typesafe than the good old Pascal counterparts. Oh, and you can easily
add code to convert strings to enum values, too.
Walter can delete all that buggy enum code from his compiler and from
the language specification.
Jun 16 2009
grauzone:I also find it ironic how Walter tries to keep D C compatible (that is, C code compiled by dmd either runs correctly, or compilation fails), while such differences between D1 and D2 seem to be OK. (Examples: const, memory allocating closures, overloading rules) Jun 16 2009
dsimcha wrote: Jun 16 2009
Robert Fraser: Jun 16 2009
"bearophile" <bearophileHUGS lycos.com> wrote in message news:h18j5t$18q7$1 digitalmars.com...Robert Fraser: Jun 16 2009
|