home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16389 < prev    next >
Encoding:
Text File  |  1992-11-17  |  5.9 KB  |  126 lines

  1. Xref: sparky comp.lang.c++:16389 comp.std.c++:1576
  2. Newsgroups: comp.lang.c++,comp.std.c++
  3. Path: sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!darwin.sura.net!convex!news.utdallas.edu!corpgate!bnrgate!bnr.co.uk!pipex!warwick!bham!bhamvx!mccauleyba
  4. From: mccauleyba@vax1.bham.ac.uk (Brian McCauley)
  5. Subject: Re: Proposal - enhancement for switch statement.
  6. Sender: usenet@rs6000.bham.ac.uk (USENET News Service)
  7. Message-ID: <1992Nov16.213015.1@vax1.bham.ac.uk>
  8. Date: Mon, 16 Nov 1992 21:30:15 GMT
  9. Lines: 113
  10. References: <1992Nov16.004558.9855@muddcs.claremont.edu>
  11. Organization: University of Birmingham
  12.  
  13. In article <1992Nov16.004558.9855@muddcs.claremont.edu>, dfoster@jarthur.claremont.edu (Derek R. Foster) writes:
  14. > In article <MCGRANT.92Nov15132747@rascals.stanford.edu> mcgrant@rascals.stanford.edu (Michael C. Grant) writes:
  15. >>In article <1992Nov15.190830.11622@cssc-syd.tansu.com.au>
  16. >>pete@cssc-syd.tansu.com.au (Peter Alexander Merel) writes:
  17. >>   ...
  18. >>   switch to use operator== rather than relying on conversion to int, so that 
  19. >>   I could use it to test cases on objects rather than building an industrial 
  20. >>   strength if statement. 
  21. > ...
  22. > Personally, I would like to see a few more changes made to the 'switch'
  23. > statement (in my opinion, one of the ugliest warts on the face of the
  24. > C language, and regrettably C++ also.)
  25. > I wouldn't advise actually getting rid of the 'switch' statement (it is
  26. > needed for backwards compatibility), but instead creating a (somewhat) 
  27. > parallel statement, called, say, 'switchc'.
  28.                                     ^^^^^^^
  29. No. Please not another token in the parser.
  30. >   1) Remove the requirement for a 'break' after each statement (the accidental
  31. >      omission of which is the cause of a ridiculously large number of errors
  32. >      within C programs.) Make a 'break' the default, rather than fallthrough
  33. >      being the default. Add a 'fallthrough' keyword (or extend the
  34. >      meaning of the 'continue' keyword perhaps?) to still allow programmers
  35. >      to use fallthrough in those rare occasions for which it is necessary/
  36. >      desirable.
  37. I agree the default behavior is wrong but were all used to it so lets keep it.
  38. All that is needed is to _advise_ compiler implementors that they should
  39. give a warning if `case' (or `default') is preceded by anything other than
  40. `break;' or `case:' (except of course the first one). Good C++ programmers
  41. treat warnings as errors unless they are compling C sources.
  42. While were about it perhaps we should point out that compliers should warn
  43. when variables defined in one case are used in another (IMHO each case
  44. should always be given it's own scope).
  45. >   2) Allow multiple labels for each 'case' statement, and extend the case
  46. >      statement to allow ranges and multiple arguments (as in Pascal.) This
  47. >      avoids the excessively verbose lists of 'case' statements that are
  48. >      necessary in C to represent ranges of values.
  49. Multiple arguments - agian I'd agree if we were inventing a language from
  50. scratch but I'm not sure there's any need to change the syntax for
  51. a saving of 3 characters. Likewise for ranges would (see below).
  52. > as per Peter's suggestion, we could easily add...
  53. >   3) for class-typed switching variables, we could require that 'switchc' 
  54. >      use operator == and a simple linear search to work its way down the
  55. >      list. (yes, this is possibly inefficient. It is, however, extremely
  56. >      clear code which is easy to read.) In the case of non-class-typed
  57. >      switching variables, the compiler is allowed to use a jump table,
  58. >      binary search, or whatever seems appropriate.
  59. >   4) in implementing 3, we could allow, for class typed variables, the
  60. >      switch labels to be any data type for which operator == with the
  61. >      given class type is defined. For instance, char *'s, floating point
  62. >      values, (references to const objects?,) etc.
  63.    ...
  64. >                           ...using 'if's, it is a) much harder to
  65. >     read, b) much harder to understand (the reader may not immediately
  66. >     see the overall structure), and c) more error-prone (it is possible to
  67. >     get mismatched 'else's, etc. which will compile fine, but not do
  68. >     what they're supposed to.
  69. >     I would LOVE to see the addition of something like this to the language.
  70. >     Control flow in many of my programs would be vastly simplified, and I
  71. >     think the number of errors I would make would be substantially smaller.
  72. Yes I agree but let's non pollute _everyones_ namspace by adding a new token.
  73. The only problem is that if a class has both an `operator==(int)' and an
  74. `operator int ()' defined inconsistantly the some existing switch statements
  75. would change their meaning.
  76.  
  77. Now (as promised) why ranges would be unnecessary (following the "if you
  78. can do it with a class then don't extend the language" rule ;-) )
  79.  
  80. template <class T> class Range {
  81.   T top,bottom; 
  82. public:
  83.   Range(t,b) : top(t),bottom(b) {} 
  84.   int operator==(T t) { return bottom <= t && top >= t; }
  85. }
  86. template <class T> inline operator == (T& t,Range<T>& r) {return r == t;}
  87.  
  88. switch (i) {
  89.   case Range<char>('0','9') : case '-' : {/*...*/} break;
  90. }
  91.  
  92. compare:
  93.  
  94. switchc (i) {
  95.   case '0'...'9' , '-': /*...*/
  96. }
  97.  
  98. OK so it's not as concise as your form but it far more C++. Assuming your
  99. compiler supports inlining it can be made to optimize as well as the
  100. explicit range syntax.
  101.  
  102. BTW heres a new constuct (my mother's favorate in BASIC) that would become
  103. available:
  104.  
  105. switch(TRUE) {
  106.   case expr1: { /* code */} break;
  107.   case expr2: { /* code */} break;
  108.   delfault: {/* code */}
  109. }
  110.  
  111. This is exactly equivalent to an if ladder but some people think it looks
  112. nicer :-)
  113. -- 
  114.     \\   ( )    No Bullshit!     |  Email: B.A.McCauley@bham.ac.uk   
  115.  .  _\\__[oo        from         |  Voice: +44 21 471 3789 (home)
  116. .__/  \\ /\@    /~)  /~[   /\/[  |    Fax: +44 21 625 2175 (work)
  117. .  l___\\      /~~) /~~[  /   [  |  Snail: 197 Harborne Lane,
  118.  # ll  l\\    ~~~~ ~   ~ ~    ~  |         Birmingham, B29 6SS, UK
  119. ###LL  LL\\   (Brian McCauley)   |   ICBM: 52.5N 1.9W
  120.