Bug 3999 and 4261

Daniel Gibson metalcaedes at gmail.com
Tue Aug 31 16:45:40 PDT 2010


bearophile schrieb:
> A way to tell them apart is the presence of the EnumTag, if it's present, then the enum is a new type:
> 
> enum V1 = 10;
> enum { V2 = 20 }
> enum Foo { V3 }
> void main() {
>     assert(V1 == 10);    // OK
>     assert(V2 == 20);    // OK
>     assert(Foo.V3 == 0); // ERROR, type mismatch
> }
> 
> Bye,
> bearophile

I sometimes find it convenient to just treat those values as integers, 
e.g. when reading/writing them with a stream (to a file, to some other 
process with SocketStream, ...).
Like
---
int x;
stream.read(x); // btw: this often sucks - why not x = stream.readInt()?
if(x == MyEnum.DIE)
	return 0;
else if(x == MyEnum.CALLFOO)
	foo(x);
---
or something like that (switch() might be better, but whatever).

Also treating named and unnamed enums differently seems inconsistent to me.

Cheers,
- Daniel


More information about the Digitalmars-d mailing list