"C" wish list. (nested procedures?)

Dick Dunn rcd at opus.UUCP
Thu Oct 24 16:23:30 AEST 1985


> Since the current arguments seem to have broken down, how about something new:
> what features would you add to 'C'...
> ...
> 	4. Allow true block structuring:
> 
> 		outs(s) char*s; {
> 			outc(c) char c; {...}
> 			tputs(s, outc);
> 		}

Let's call this "nested procedures" since C has its own sort of block
structure.  I would be very happy <<never>> to have to deal with nested
procedures again.  (I have had to deal with them more from the compiler-
writer's side than from the user's side.)  Nested procedures are neither
easy nor cheap (unless done wrong, which happens often enough).  They fight
particularly with procedure variables, which I consider to be one of C's
more useful features (and a moderately serious shortcoming of Pascal).

First, what are the advantages of nested procedures?  Two main ones:

	Procedure hiding--Inner procedures are local to the outer one,
	which keeps them out of the scope of people who shouldn't be using
	them--but there are better ways to get this protection, and the
	nesting (contour model) isn't necessarily the best anyway.

	Access to intermediate variables--the locals of an outer procedure
	are global to inner procedures.  There are more arguments that
	use of this mechanism leads to hard-to-read code than that it is
	useful.

Second, what are the costs?  The major cost is that it is necessary to
maintain the static linkage of procedures--a record of which stack frames
from outer levels are accessible at inner levels.  Either it has to be
maintained by updating at each procedure call (which makes procedure calls
much more expensive than a jsr or so) or it has to be dug out in order to
access intermediate variables.  Worse, when you pass a procedure as a
parameter, you have to pass the static chain along with it.  This means
either a reference to static chain information stored somewhere, with
beaucoup magic to reference intermediate variables, or you carry the entire
static chain with you; procedure objects get large and you have to present
an edict on the maximum static nesting of procedures.

The worst of it is that <<nested procedures are seldom necessary>> IF a
language provides other hiding mechanisms.  Simple as it is, C's rule for
limiting the scope of static objects to a compilation unit is entirely
adequate for this sort of hiding.


-- 
Dick Dunn	{hao,ucbvax,allegra}!nbires!rcd		(303)444-5710 x3086
   ...At last it's the real thing...or close enough to pretend.



More information about the Comp.lang.c mailing list