Xref: utzoo comp.std.c++:258 comp.std.c:3519 Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!cs.utexas.edu!usc!snorkelwacker!bloom-beacon!world!burley From: burley@world.std.com (James C Burley) Newsgroups: comp.std.c++,comp.std.c Subject: Proposed Enhancement to select/case (yes, I know...) Message-ID: Date: 30 Aug 90 10:06:45 GMT Sender: burley@world.std.com (James C Burley) Distribution: comp Organization: The World Lines: 107 After seeing some of the discussions about what can't (or shouldn't) be added to C/C++ select/case construct capabilities, I'd like to post my thoughts on something that might actually be useful and should be easy to implement. First off, this proposal applies to both C++ and C. I don't suggest adding any new types; run-time expressions on cases; class types on selects/cases; or anything like that. Plenty of people have provided excellent explanations of why those features would not be desirable. But how about this: allow ranges (and, perhaps, lists) on case statements. I'll pick a syntax for now -- using brackets, though perhaps someone has a better idea. Here's an example: select(foo) // Nothing new here. { case ALPHA: // Nothing new here. ... case [BETA:GAMMA]: // Matches any value for foo where BETA<=foo<=GAMMA. ... case [:MINIMUM,MAXIMUM:]: // Matches foo<=MINIMUM or foo>=MAXIMUM. ... case [EPSILON,OMEGA]: // like "case EPSILON: case OMEGA:". ... case [DELTA:PI,TAU]: // like "case [DELTA:PI]: case TAU:". ... } To summarize, a case statement may have a comma-separated list of case-range-exprs (at least one item in the list) within brackets instead of the usual integral constant expression. Each case-range-expr is either an integral constant expression (called "int-expr" subsequently), "int-expr:", ":int-expr", or "int-expr:int-expr". Within a switch construct, only one case-range-expr with the form ":int-expr" is permitted, and only one with the form "int-expr:" is permitted. If two case-range-exprs exist, one with the each of these forms, "default" is not permitted (or, it could be interpreted as a null range, described below, if people want). If a given case-range-expr effectively specifies a null range, it is considered a null case range. Null case ranges are permitted. A null range occurs if the int-expr in ":int-expr" is less than the minimum value representable by the type of the expression in the select statement; if the int-expr in "int-expr:" is greater than the maximum value representable; or, for a range of the form "lowest:highest", lowest>highest, lowest>maximum-value, or highest functions (macros). I'm not going to say it's any more portable, however; in fact it is less portable than using or just listing all the letters (and digits?) individually. It's a "hacker's example" of the utility of case ranges; a more realistic example is hinted at in the first sample in this posting (where the source code containing the select is not under the same control as that defining the constants in the case statements). A thought: although I still shiver at the idea of allowing float or double types on a select, the availability of ranges somewhat lessens the arguments against it: the language could disallow any case specification (other than, perhaps, the constant 0.) was not a range. However, once one has floats, one next wants ways to say "foo<0.", "foo==0.", "foo>0.", for example, somehow extending the syntax to specify "<" or ">" comparisons instead of "<=" or ">=". And I think that suggests floats are going too far. Anyway, the basic idea of case ranges (and lists of cases) comes from Fortran 90, as many of you already know. Please don't assume that it therefore is a bad idea! Does anyone know any good reasons NOT to implement some or all of these features in today's C++ and C compilers, with an eye towards codifying them in the next ANSI standards for these languages? I wouldn't suggest putting them into the standards until people had had a couple of years to try them out in real code...if they're not useful, let's not add the extra baggage; at least that's my philosophy as a C programmer! James Craig Burley, Software Craftsperson burley@world.std.com