www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How does enums work?

reply MicroWizard <MicroWizard_member pathlink.com> writes:
When I declare an enum's enclosing integral type,
why does try the compiler to use cast?

Operators '|=' and '&=' works but a simple assign
-which should be identical- does not.

Have I missed something important?

Tamas Nagy

---------------------------------------------------------
enum TWDF:ubyte{
VALID=0x80
}

class TWdp{
TWDF dataflags;
public:
//getter
bit valid() {(dataflags&TWDF.VALID)==TWDF.VALID;}

//setter
void valid(bit v)
{
dataflags= dataflags|TWDF.VALID;
//types.d(14): cannot implicitly convert expression (cast(int)(this.dataflags)
//| 128) of type int to TWDF

dataflags= dataflags&~TWDF.VALID;
//types.d(16): cannot implicitly convert expression (cast(int)(this.dataflags)
//& 127) of type int to TWDF

dataflags=v?(dataflags|TWDF.VALID):(dataflags&~TWDF.VALID);
//types.d(18): cannot implicitly convert expression (v ?
//cast(int)(this.dataflags) | 128 : cast(int)(this.dataflags) & 127) of
//type int to TWDF

/* this one works
if(v)
{
dataflags|=TWDF.VALID;
}
else
{
dataflags&=~TWDF.VALID;
}
*/	}
}
Apr 24 2005
parent Derek Parnell <derek psych.ward> writes:
On Sun, 24 Apr 2005 22:30:48 +0000 (UTC), MicroWizard wrote:

 When I declare an enum's enclosing integral type,
 why does try the compiler to use cast?
 
 Operators '|=' and '&=' works but a simple assign
 -which should be identical- does not.
 
 Have I missed something important?
Seems like a bug to me too. I would have thought that neither '|=' or '=' should have worked. I'm thinking that if you have an element of an enum set modified by ORing or ANDing it with some arbitrary bit pattern, then the resulting value is not guaranteed to be a member of the same enum set anymore, and so it should fail to compile. For example, should this compile ... dataflags = 55 | TWDF.VALID; I think you should be using some other construct to acheive the effect you are after, else define two elements of the enum set and use them thus ... enum TWDF:ubyte{ VALID=0x80, INVALID=0x7F } void valid(bit v) { dataflags = (v == 1 ? TWDF.VALID : TWDF.INVALID); } -- Derek Parnell Melbourne, Australia 25/04/2005 9:17:04 AM
Apr 24 2005