Proposed Enhancement to select/case (yes, I know...)

James C Burley burley at world.std.com
Sat Sep 1 17:52:53 AEST 1990


In article <1990Aug31.134248 at ee.ubc.ca> mikeb at ee.ubc.ca (Mike Bolotski) writes:

   In article <1990Aug30.164610.3519 at zoo.toronto.edu>, henry at zoo.toronto.edu (Henry Spencer) writes:
   > In article <BURLEY.90Aug30030645 at world.std.com> burley at world.std.com (James C Burley) writes:
   > >... how about this: allow ranges (and, perhaps, lists) on case statements.
   > 
   > Such a feature appeared in one draft of ANSI C, and disappeared in the
   > next.  I believe the reason was the usual:  there was no implementation
   > experience with it, and it was a minor convenience rather than a solution
   > to a serious problem.

   From the G++ info file:

   Switch Ranges
   =============

   A GNU C++ extension to the switch statement permits range specification
   for case values.  For example, below is a concise way to print out
   a function parameter's "character class:"

	print_char_class (char c) 
	{
	  switch (c)
	    {
	    case 'a'..'z': printf ("lower case\n"); break;
	    case 'A'..'Z': printf ("upper case\n"); break;
	    case '0'..'9': printf ("digit\n"); break;
	    default:       printf ("other\n");
	    }
	}

   Duplicate, overlapping case values and empty ranges are detected and
   rejected by the compiler.

   --
   Mike Bolotski          VLSI Laboratory, Department of Electrical Engineering
   mikeb at salmon.ee.ubc.ca University of British Columbia, Vancouver, Canada 

Ok, great, then we can all forget about my original recommendation.  Right
after the posting I began thinking that the [x:y]: syntax I proposed was
obnoxious, and though it isn't really ambiguous (because "?" isn't by itself
an operator), use of the colon in this new way might bother some people (though
of course it already is used in this way -- consider "case x?y:z:", where x,
y, and z are all constants).  I thought maybe "..." or ".." would be a better
separator.

Sure enough, somebody emailed me with the very same comments (not a user of
GNU CC), and we entered into a discussion of various syntax possibilities.
(One issue was that the list feature, as in "case 1,2,3:", would make a second
case I know of where the comma operator is "turned off" to support a different
use of comma and thus requires an expression using the comma operator in that
context to be placed in parens -- the first case is "foo(1,2,3)", a function
invocation.)

So I was considering reposting my proposal with this modified syntax, but
here somebody points out that GNU CC already has it!!  This is all that is
needed.  As I said (at the end of my original posting), I expect this feature
would be added to the C/C++ standard only if existing practice proved it
useful.  GNU CC establishes existing practice: useful is established (later)
by comments received by the standards committee, or perhaps seeing more vendors
of other C compilers add the same features to be GNU compatible (due presumably
to demand by customers/marketing).

I still have two questions: does "case ..MINIMUM:" match any value of the
expression less than or equal to MINIMUM, and "case MAXIMUM..:" accordingly
match expr>=MAXIMUM and, if not, would these be useful features?

Also, why didn't anybody point out how stupid it was for me to say that if
both minimum and maximum cases were specified (i.e. "case ..MINIMUM:" and
"case MAXIMUM..:" in a switch), "default" could not be specified!  (Sigh, I
forgot about the need to catch "hole" values not specified by "case"
statements for values between MINIMUM and MAXIMUM....)  In any case, thanks
for not jumping all over me about that.

Someday I've got to get a UNIX box and start using GNU software.

James Craig Burley, Software Craftsperson    burley at world.std.com



More information about the Comp.std.c mailing list