[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon Jan 26 03:39:34 1993 
Subject:Re: dlocals etc. (and shallow binding) 
From:Ray Dunn 
Volume-ID:930127.02 

In refd article, axs@cs.bham.ac.uk (Aaron Sloman) writes:
>A really "clean" implementation would have required all local
>variables to have their values initialised to some safe default on
>procedure entry, e.g. to "undef".

...and to take advantage of internal knowledge of the fact that it doesn't
raises the hackles of anyone concerned about "future release surprise" or
portability!

>    define print_procedure( ..... );
>        dlocal printlevel = printlevel + 1;


looks pretty bogus to me.  I've been trying to teach programmers to avoid
this sort of thing for years.  POP-2 was quite clear about it, even though
this code would "work".  The reference manual states that when the extent
of another variable is interrupted "its value is not altered but it cannot
be accessed or changed by assignment."

>The alternative to these dynamically scoped uses of printlevel and
>popmatchvars (and similar things, like interrupt, cucharout,
>prmishap, etc.) would have been to require all these values to be
>passed explicitly as extra arguments in any procedure that might
>call a procedure that used these values, or else to do what Robin
>does, namely:
>
>> My own practice these days is to avoid any dlocals in code I actually
>> write, by using an extra argument to any function (usually called "Env")
>> which is a data-structure that bundles up any kind of style switch.

In 'C' the effect can often be achieved by using a _static_ variable, but
my own technique is to use a pair of functions - the outer function
presents a "clean" interface to the outside world and calls a second
function with all the required extra arguments - it is this second function
that recurses.

So Aaron's example becomes (ha! I'll do it in POP-2ish)

    vars max_printlevel;
    5 -> max_printlevel;

    print_procedure(....)
        aux_print_proc(..... , 1);
    end

    aux_print_proc(..... , printlevel)

        if printlevel >= max_printlevel then
            ... print closing bracket and return ....
        else
            ... print suitable opening bracket ....
            aux_print_proc(whatever tail of structure, printlevel+1);
        close
    end

Now who has a compiler I can syntax check this with!!
-- 
Ray Dunn at home        |  Beaconsfield, Quebec  |  Phone: (514) 630 3749
ray@philmtl.philips.ca  |  ray@cam.org           |  uunet!sobeco!philmtl!ray