switch case for constants-only?

Rainer Deyke rainerd at eldwood.com
Sat Dec 5 19:06:41 PST 2009


Nick Sabalausky wrote:
> "Rainer Deyke" <rainerd at eldwood.com> wrote in message 
> news:hfeu0m$1af9$1 at digitalmars.com...
>> 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.

The question is how to make 'goto case' work with the improved switch
statements without breaking compatibility or creating an artificial
distinction between static and dynamic switch statements.  The obvious
solution is to rewrite this:

  switch (n) {
  case a:
    goto case b;
  ...
  }

...as this:

  __switch_value = n;
__switch_start:
  switch (__switch_value) {
  case a:
    __switch_value = b;
    goto __switch_start;
  ...
  }

This rewrite preserves the current semantics of 'goto case' while
allowing the case label to be an arbitrary expression, both in the
actual label and in the 'goto case' statement.  The "computed goto"
effect is a natural consequence of the rewrite.

>> It also fails to
>> address this:
> 
> I don't see why it would need to.

It doesn't need to, but it makes 'switch' statements slightly more
error-prone.  That's a small but real and measurable loss.

>> goto case;
> 
> Ahh, yea, good point. Does that currently work?

It should.  I don't have a D compiler around to test it.

-- 
Rainer Deyke - rainerd at eldwood.com



More information about the Digitalmars-d mailing list