switch case for constants-only?

Nick Sabalausky a at a.a
Sat Dec 5 16:57:15 PST 2009


"Rainer Deyke" <rainerd at eldwood.com> wrote in message 
news:hfeu0m$1af9$1 at digitalmars.com...
> Nick Sabalausky wrote:
>> "Rainer Deyke" <rainerd at eldwood.com> wrote in message
>> news:hfenkl$veq$1 at digitalmars.com...
>>> I assume the same rule applies to 'goto case'?
>>
>> I think that amounts to a computed goto, which I don't think D currently
>> has, so that probably just wouldn't compile.
>
> So case labels could be variables but labels for 'goto case' would be
> constant?  That seems backwards and inconsistent.

I guess I don't see it as inconsistent because I don't see case labels quite 
so much as goto labels but moreso as just an (awkward) syntax for pattern 
matching.

> It also fails to
> address this:
>
> int i = 0, j = 0;
> switch (j) {
> case i:
>  break;
> case j:
>  // Never reached.
> }
>

I don't see why it would need to. Switch compares values, not identifiers. 
If that needed to be addressed, then this would also need to be addressed:

int i = 0, j = 0;
if(j == i)
    {}
else if(j == j)
    { /+ never reached +/ }

And maybe you could argue that does need to be addressed too, but at that 
point it's no longer an issue of switch accepting or rejecting run-time 
values.

>> But with or without computerd goto, this sounds like a reason to use an
>> actual fallthrough command instead of "goto case".
>
> Not really.  If you want fallthrough, use 'goto case' without a label,
> like this:
>
> goto case;
>

Ahh, yea, good point. Does that currently work?





More information about the Digitalmars-d mailing list