generalized switch
karl at haddock
karl at haddock
Fri Aug 8 02:54:00 AEST 1986
barmar at mit-eddie.MIT.EDU (Barry Margolin) writes:
>It might, however, be reasonable to extend the case statement to not
>require all the cases to be constants. This would still provide the
>first two features I listed; the compiler would have to do a bit more
>work to determine if the construct can be translated into a jump table.
But switch as currently implemented always matches exactly one case
(assuming an implicit "default: break;" if necessary). I like switch to be
commutative, i.e. case blocks to be interchangeable; I think this idea is a
step in the wrong direction. (I realize that switch is not quite
commutative now, because of fallthrough. I'd like to see this "feature"
phased out, too. "Warning: case label entered from above"?)
Now, some constructive counterproposals.
I think one should be able to specify a list. "case 1,4,10:" is neater than
"case 1: case 4: case 10:", and shoots down the obvious objection to my
comment about fallthrough. (Yes, I know there are more subtle objections.
I'm willing to use a goto for them, if switch gets cleaned up.)
Better yet would be a list of ranges. "case 'a' to 'z', 'A' to 'Z':"
requires 52 labels in the current syntax; this is where I normally rewrite
as if-else. (Yes, I know that's better anyway because I can use the more
portable test isletter(). But there are other uses for range cases, and
one could always "#define UPPER 'A' to 'I', 'J' to 'R', 'S' to 'Z'" for
EBCDIC systems, to retain portability.) It should also be possible to
specify open-ended ranges, to allow things like "switch (strcmp(s, t)) {
case LT: ... case EQ: ... case GT: ... }" where LT and GT are #define'd as
intervals extending to minus and plus infinity.
Syntactic issues: I introduced a new keyword "to" in the above. This should
be a punctuation mark instead, but "-" already has meaning in this context.
(If backward compatibility were not a problem, "case 'a'-'z':" could still
be interpreted as a range, and expression containing minus would have to be
enclosed in parens -- but that would be too confusing.) The ellipsis mark
"..." could be used, I suppose; I don't see any confusion with the varargs
function prototype notation. Interval notation like "['a','z']" is another
interesting possibility*; it allows for both open "()" and closed "[]"
intervals, as well as mixed: "case [0,N): return a[i];". But I don't think
the user community is ready for misbalanced hooklets**.
Btw, note that the use of comma is not a problem since the comma operator is
not permitted in constant expressions***. (Nor would it be useful.)
Karl W. Z. Heuer (ihnp4!ima!haddock!karl), The Walking Lint
* "[a,b)" is the American notation for a half-open interval. The European
notation is "[a,b[", which would be even worse.
** hooklet: generic bracket. See discussion in net.lang.
*** According to X3J11 draft of May, 1986 (page 48, line 12).
More information about the Comp.lang.c
mailing list