www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - WithStatement for enums

reply Deewiant <deewiant.doesnotlike.spam gmail.com> writes:
I don't know about the rest of you, but I, at least, often tend to try to use
enums somehow like this:

--
enum X { A, B, C }

void main() {
	X x = A;
}
--

Uh oh! "Undefined identifier A" etc.

This came up a while ago in a switch statement where I was testing the state of
an enum. On a whim, I tried the following:

--
switch (x) with (x) {
	case A:
		...
	...
}
--

Only to get the error "with expressions must be class objects, not 'X'". I
checked the docs and got the added information that structs work, too, but enums
don't.

Funnily enough, this is the only case I can recall when I've ever wanted to use
the with statement, and surely enough it doesn't work. Typical. <g>

How about it? Why not make WithStatement work with enums, in the manner shown
above? I, for one, would find it handy.
May 12 2006
parent =?UTF-8?B?SmFyaS1NYXR0aSBNw6RrZWzDpA==?= <jmjmak utu.fi.invalid> writes:
Deewiant wrote:
 How about it? Why not make WithStatement work with enums, in the manner shown
 above? I, for one, would find it handy.

Java allows enum "members" without the enum "class" name prefix in switch-case-statements. Maybe this would be possible in D too without greater problems: enum X { A, B, C } ... X x = X.A; switch(x) { case A: ... case B: ... } Currently both Interfaces and Enums are a bit like second class citizens in D. Hopefully they are "fixed" before DMD 1.0 is out. :) -- Jari-Matti
May 12 2006