home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / std / cplus / 1901 < prev    next >
Encoding:
Text File  |  1992-12-23  |  8.2 KB  |  193 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re: C++ already *has* nested functions SO WHAT'S THE BEEF?
  5. Message-ID: <1992Dec23.165719.13021@ucc.su.OZ.AU>
  6. Keywords: nested functions, dumb ideas
  7. Sender: news@ucc.su.OZ.AU
  8. Nntp-Posting-Host: extro.ucc.su.oz.au
  9. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  10. References: <1992Dec21.080952.15309@netcom.com>
  11. Date: Wed, 23 Dec 1992 16:57:19 GMT
  12. Lines: 179
  13.  
  14. In article <1992Dec21.080952.15309@netcom.com> rfg@netcom.com (Ronald F. Guilmette) writes:
  15. >
  16. >The example below clearly shows that this is an entirely viable way to
  17. >achieve nested functions with C++ (as now defined).  
  18.  
  19. >goes to great lengths to demonstrate that references to named entities
  20. >declared locally within the function containing the "nested" function
  21. >are possible (and legal) with a few minor exceptions, as long as the
  22. >references in question obey the normal name scoping rules.
  23.  
  24.     The principal purpose of nested functions is to
  25. access local (auto) data in the containing scope, and thats one of the
  26. exceptions you list.
  27.  
  28.     Futhermore, I want to nest functions inside MEMBERS,
  29. so these functions get access to the 'this' pointer and the
  30. objects other members (with the same privileges as the containing
  31. member), which implies the function cant be static.
  32.  
  33. >The problems with this technique are basically as follows:
  34. >
  35. >    o    We have to "invent" an otherwise useless and unnecessary
  36. >        containing struct, union, or class type in order to be
  37. >        able to nest functions.
  38.  
  39.     I'd live with that.
  40. >
  41. >    o    Due to the (silly?) prohibition against defining a function
  42. >        member of a block-local struct, union, or class type outside
  43. >        of (and after) its "parent" type, and due to the (silly?)
  44. >        rule that says that all functions defined *within* a class
  45. >        are implicitly `inline', our nested functions are always
  46. >        necessarily `inline' (when using this technique for nesting)
  47. >        even if we do not want them to be.
  48.  
  49.     Irrelevant, dont care, as long as I can still take its address.
  50.  
  51. >
  52. >    o    Last but not least, although nested functions can legally
  53. >        reference *most* names of *most* kinds of things declared
  54. >        in a containing scope (including one local to a containing
  55. >        function) there is one small and mildly annoying exception
  56. >        to this general rule... i.e. we cannot access `auto' and
  57. >        `register' *objects* declared within a containing local
  58. >        scope.  
  59.  
  60.     This the whole purpose of nested functions. It is not a minor
  61. and mildly annoying exception.
  62.  
  63. >
  64. >I believe that it is this last item that concerns people the most.  In
  65. >fact, I get the general impression that some people view these minor
  66. >exceptions (to the general scoping rules) as being horrendous problems
  67. >which are so utterly insurmountable that dramatic language changes are
  68. >required in order to eliminate these exceptions.
  69.  
  70.     Indeed this is the problem that concerns me most with your
  71. technique. No dramatic language change is required, its basically
  72. the removal of a restriction resulting in a *simplification*
  73. of the language syntactically.
  74.  
  75.     Its been done by GNU. 
  76. >
  77. >It would not concern me if the kinds of changes being suggested were
  78. >(in general) trivial for implementors to implement, but when I hear about
  79. >all of these complicated schemes for supporting dynamic "up-level addressing"
  80. >and also such things as "special" kinds of function pointers for holding
  81. >pointers to nested function (and a special new syntax for declaring such
  82. >things) I shrink back in horror.  
  83.  
  84.     Why are you scared of one of the few things in computer
  85. science that is totally and comletely understood, and that is
  86. routinely taught to second year CS students? It is so established
  87. that both the 386 and 68000 have hardware to support this
  88. mechanism.
  89.  
  90. >Haven't the implementors already got
  91. >enough to do??  Can't people figure out how to simply pass any needed
  92. >`auto' variables which are local to the containing function as additional
  93. >actual argument to the nested (called) function on those very rare occasions
  94. >when this is actually necessary??  I mean what the devil is the problem here?
  95.  
  96.     If you keep adding new variables, and have several
  97. pretend nested functions with parameters,  you have in effect
  98. to duplicate the entire set for each function. Apart from
  99. the horrid maintenance problem, its also extremely
  100. inefficient.
  101. >
  102. >So why do people insist on attacking (and defeating) the third minor
  103. >problem listed above (regardless of costs)?
  104.  
  105.     Because accessing auto variables is exactly the reason
  106. why we want nested functions. I hadn't even *thought* of 
  107. accessing anything else, never occured to me that I could
  108. already access a local class name, because I dont care about
  109. that if I cant access the local variables.
  110.  
  111. >
  112. >Well, it appears that some people have this mental model of "nested
  113. >functions" left over from the days when they programmed in Pascal, where
  114. >*anything* in a containing scope could be accessed directly.  
  115.  
  116.     In my case, yes. I liked that ability very much.
  117. Every block structured language should support it. It can
  118. be overused, like anything else.
  119.  
  120. >Sadly, these
  121. >folks don't seem to "get" the fundamental idea which made C into the big
  122. >success that it is today... i.e. that it is *good* when potentially
  123. >expensive run-time actions (e.g. indirection through some number of
  124. >"static link" pointers) are made *explicit* to the programmer so that he
  125. >or she will be more aware of the potential effects these things may have
  126. >upon run-time performance.
  127.  
  128.     I dont agree that the language should force the user to
  129. type letters in proportion to the execution time of the program
  130. to remind them how slow its going to be.
  131.  
  132.     And you are completely wrong about the expense of accessing
  133. variables, on the 386 for example the outer scopes at ANY level
  134. can be accessed with exactly one extra indirection.
  135. The technique is called a 'display'. The algorithm for maintaining
  136. the display is trivial and implemented on the 386 and by
  137. hardware. It can be done almost as easily with software.
  138. What is horribly inefficient is passing lots of parameters
  139. needlessly.
  140.  
  141. >
  142. >Obviously, I have a strong disagreement with the folks who would (for the
  143. >sake of eliminating a very minor restriction which only makes a difference
  144. >in rare cases anyway) "hack" the language mercilessly (and thereby cause
  145. >yet another headache for implementors).
  146.  
  147.     Why is eliminating the restriction against nested functions
  148. a hack? You can nest everything else, why not functions?
  149. >
  150. >I mean c'mon folks... This endless "featurism" has got to stop somewhere!
  151.  
  152.     Agreed. Lets get rid of the feature that specifically
  153. prohibits nested functions. We already have nested blocks,
  154. and a nested function is just a named nested block
  155. with parameters.
  156.  
  157.     A language that supports stuff as complicated as pointers
  158. to members, multiple inheriance with virtual and nonvirtual bases,
  159. virtual functions, nested classes etc etc should have no 
  160. problems supporting something as simple as nested functions.
  161. Compared to the problems of the type system, nested functions
  162. just aren't that hard.
  163.  
  164. It has been done by GNU,
  165. it can be done easily, 
  166. it can be highly efficient,
  167. it requires almost no new syntax, 
  168. it is quite useful,
  169. it simplifies the language by removing a restriction,
  170. it is within the spirit of C to provide facilities that
  171.   match the underlying hardware, 
  172.   and most CISC machines have this hardware,
  173. it can be implemented without hardware support on other machines,
  174. it breaks no existing code,
  175. it doesnt cost you anything if you dont use it.
  176.  
  177. If this were a MAJOR extension to the language (like exceptions)
  178. I would agree it wouldn't be useful enough to add, but that
  179. isnt the case.
  180.  
  181. Small conveniences are just the things programmers are usually
  182. most grateful for. (Anyone for switch .. case 'a','d'..'f':  :=)
  183. Remember the language is for programmers to use, the vendors
  184. and committee's convenience is not so important as the standard
  185. for the world most popular language, which we're going to get stuck
  186. with for the next 20 years :-).
  187.  
  188. -- 
  189. ;----------------------------------------------------------------------
  190.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  191.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  192. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  193.