home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11574 < prev    next >
Encoding:
Text File  |  1992-07-26  |  8.7 KB  |  192 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!mcsun!Germany.EU.net!gmd.de!jvnc.net!darwin.sura.net!mips!mips!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  3. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  4. Subject: Re: New C++ type: boole
  5. Message-ID: <9220903.24461@mulga.cs.mu.OZ.AU>
  6. Sender: news@cs.mu.OZ.AU
  7. Organization: Computer Science, University of Melbourne, Australia
  8. References: <DOUGM.92Jul25234214@titan.cs.rice.edu>
  9. Date: Sun, 26 Jul 1992 17:59:44 GMT
  10. Lines: 180
  11.  
  12. dougm@titan.cs.rice.edu (Doug Moore) writes:
  13.  
  14. >P. S.  The latest draft of the proposal follows.  Constructive
  15. >feedback is still welcome.  Shall I officialize it and send it off to
  16. >our beloved standards committee?
  17.  
  18. Um, well, no.
  19. Without intending to insult anyone, I have to say that this proposal has 
  20. so many flaws that I don't know where to start. I couldn't be bothered replying
  21. the first time, reasoning that someone was bound to shoot it down soon enough
  22. anyway :-). But before you send it off to the committee, at the very least it
  23. needs some substantial improvements. Oh well, here goes...
  24.  
  25. >Proposed extension to C++: A Boolean type
  26. >Version 2: 26 Jul 1992
  27. >Doug Moore
  28. >dougm@cs.rice.edu
  29. >
  30. >0. Introduction
  31. >
  32. >One of the weakness of the type system that C++ inherited from C is in
  33. >the overuse of the int type.  In particular, a Boolean type
  34. >representing truth and falsehood does not exist in C, as it does in
  35. >many languages, but instead truth and falsehood are represented by
  36. >integral values 1 and 0.
  37. >
  38. >To some, the weakness of the type system is a strength of the
  39. >language, and expressions like "8 + 5*(a<b)" are part of their
  40. >programming idiom.  No proposed change to C++ can invalidate such
  41. >expressions.        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  42. ^^^^^^^^^^^^^
  43. To most of us, that point is enough to convince us that any attempt to
  44. add a boolean type to C++ is not going to buy us enough that it is
  45. worth the effort required to implement it.
  46.  
  47. >              To many, however, the weakness of the type system that
  48. >permits programming errors to go undetected is a language flaw.  The
  49. >language should provide greater type safety to those who want it.
  50. >Thus, I propose the extension of C++ to define a boolean type.  A
  51. >complete list of amendments to the base document that support the
  52. >extension appears at the end of this document.
  53. >
  54. >1. Outline of the extension.
  55. >
  56. >I propose to add to C++ a new integral type, boole, and literals of
  57. >that type, 0t, and 0f.
  58.  
  59. 0t ??
  60. This is woefully counter-intuitive. If you *have* to have syntactically
  61. special values for true and false, at least make it "1t".
  62. (What's wrong with "boole::false" and "boole::true" or some such?)
  63. C++ is hard enough on beginners already, let's not add yet another
  64. complicated and counter-intuitive syntactic construct.
  65.  
  66. >Besides paying tribute to a pioneer in the
  67. >formalization of logic, the type name is not one that has been popular
  68. >with those who have tried to simulate boolean types in the past.
  69. >Therefore, that choice of type name is less likely to break current
  70. >code than a type name like "bool" or "Boolean".  The literal names 0t
  71. >and 0f cannot appear, even accidentally, in current code.
  72. >
  73. >The boole type is generated as the result of comparison operations, as
  74. >the result of applying the logical connectives && and ||, and as the
  75. >result of applying the unary operator ! to boole, pointer and
  76. >arithmetic types.  It is used in the first-expression parts of
  77. >conditional expressions (?:), and as the test expressions in if, for,
  78. >while and do statements.  When arithmetic or pointer types appear in a
  79. >context that requires a boolean value, standard logical conversions
  80. >are applied to convert nonzero arithmetic and nonnull pointer types to
  81. >0t.  Warnings can be generated when logical conversions are applied;
  82. >such warnings should be optional early compilers, and later compilers
  83. >should warn by default but optionally disable warnings.
  84. >
  85. >The boole type is an integral type, and in a general arithmetic
  86. >context can be promoted to an integer value 0 or 1.  Integral
  87. >promotion of boole values generates warnings, optionally at first, but
  88. >eventually by default.
  89. >
  90. >2.  New and redefined operators.
  91. >
  92. >Certain operators can be usefully reinterpreted when boole values are
  93. >distinguished from other integral values.  In particular, the unary
  94. >operator ! can be read as isFalse (when applied to booles), as isZero
  95. >(when applied to arithmetic values) and as isNull (when applied to
  96. >pointers).  This proposal does not, therefore, require that warnings
  97. >be issued when ! is applied to various types.  It is hoped that by
  98. >distinguishing the three different meanings of !, confusion about null
  99. >pointers and their representation may be lessened.
  100.  
  101. So you are going to give me a warning if I write
  102.  
  103.     if (ptr) ...
  104.  
  105. but not if I write
  106.  
  107.     if (!ptr) ...   ?
  108.  
  109. Sounds very counter-intuitive to me.
  110.  
  111. >The ++ and -- operators serve a useful, if redundant, purpose in the
  112. >extension.  When applied to boole lvalues, they each invert the
  113. >lvalue.  Thus "b[<expr>] = !b[<expr>]" or, more likely, "b[<expr>] ^=
  114. >1" could be replaced by "++b[<expr>]".  Note that the use of ++ to
  115. >toggle logical values is already a common idiom, particularly in the
  116. >decoding of command line options and the corresponding setting of
  117. >flags.
  118. >
  119. >New operators &&= and ||= extend the op= paradigm, in that they permit
  120. >replacement of, for example, (b1 = b1 && b2) by (b1 &&= b2).  Such
  121. >operators could prove especially useful in a context like (b1[<expr>]
  122. >&& = b2), particularly if <expr> has side effects.  The expression
  123. >could not otherwise be written without the introduction of a temporary
  124. >variable.  Like their underlying logical connectives, these assignment
  125. >operators are short-circuiting; that is, the right hand side need not
  126. >be evaluated if the left hand side determines the result of the
  127. >operation.
  128.  
  129. Can you stick to proposing one extension at a time, please?
  130. The merit of &&=, ||= is very debateable, as is the idea of introducing
  131. a new idiom ++ for toggling logical values. (Why not just use
  132. "negate(t)" instead of "t++"? The former seems to convey the meaning much
  133. more clearly.)
  134.  
  135. >3. Additional advantages to the extension.
  136. >
  137. >Among the common errors that will generate warnings when the extension
  138. >is adopted are the confusion of assignment and equality testing (if (a
  139. >= b)) and the confusion that can arise in interval testing (if (a < b
  140. >< c)).  In general, the inadvertant mixture of arithmetic and boolean
  141. >values will be detected and corresponding bugs avoided.
  142. >
  143. >Functions will be overloadable on boolean values, which will be
  144. >convenient for some class designers.  Conversion operators "operator
  145. >boole()", "operator void*()", and "operator int()" will be able to
  146. >simultaneously coexist, which again may convenience some class
  147. >designers.
  148. >
  149. >The primary advantage is probably the resulting elimination of
  150. >progammer defined, conflicting boolean types.  That so many
  151. >programmers choose to simulate a boolean type suggests that it would
  152. >be a popular language feature.  That so many programmers do it
  153. >differently suggests that there is not a clearcut way to do it well
  154. >within the limits of the current language.
  155. >
  156. >4. Disadvantages to the extension.
  157. >
  158. >Programmers who now deliberately overload a function to take an int
  159. >argument, and then call that function with an argument that is of
  160. >boole type under this proposal, will have their programs break.  They
  161. >will have to cast the result argument to int explicitly.
  162. >
  163. >Programs that use the identifier "boole" will break.
  164. >
  165. >The warnings issued for integral promotions of booles and logical
  166. >conversion of arithmetic and pointer types may annoy some.  The
  167. >warnings can be eliminated by explicit casts or, better, by replacing
  168. ><boolexpr> by (<boolexpr>?1:0) and <arithexpr> by !!(<arithexpr>)
  169. >where appropriate.
  170.  
  171. Also you are asking for a lot of work for the compiler-writers.
  172.  
  173. To me, the advantages seem minimal (or even negative! :-) and do not
  174. justify the disadvantages.
  175.  
  176. One more comment: what about all the standard library routines?
  177. Are you going to change isdigit() etc. to return booles?
  178. Can you imagine all the conflicts you are going to get, whichever way
  179. you decide to go? I strongly suspect that even if this proposal was
  180. accepted and implemented, programmers would just disable the warnings
  181. rather than bother fixing all their old code. And since most new code
  182. has to at least interface with some old code, they will have to disable
  183. warnings for their new code too, or face heaps of spurious warnings.
  184. (The same thing happened with "const"; however the advantages of using
  185. const where overwhelming, for boole they seem underwhelming :-)
  186.  
  187. -- 
  188. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  189. This .signature VIRUS is a self-referential statement that is true - but 
  190. you will only be able to consistently believe it if you copy it to your own
  191. .signature file!
  192.