[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Default scope of references



    Date: 29 Oct 1985 18:09:05 EST (Tue)
    From: Dan Hoey <hoey@nrl-aic.ARPA>
    (DEFVAR *TEST-1* '(PROGN (DEFUN SET-FOO (VAL)
				    (SETQ FOO VAL))
			     (SET-FOO NIL)))
(eval *test-1*)
==> NIL
foo
==> NIL
    (DEFVAR *TEST-2* '(PROGN (SET 'FOO T)
			     (DEFUN GET-FOO ()
				    FOO)
			     (GET-FOO)))
(eval *test-1*)
==> NIL
foo
==> NIL
    in the absence of (PROCLAIM (SPECIAL FOO)), I somehow expected that
    (EVAL *TEST-1*) and (EVAL *TEST-2*) would each tell me that I was
    accessing a variable that was neither declared special nor lexically
    bound.  According to CLtL, however, ``The general rule is that if the
    symbol occurs textually within a program construct that creates a
    *binding* for a variable of the same name, then the reference is to the
    variable specified by the binding; if no such program construct
    textually contains the reference, then it is taken to refer to the
    special variable of that name.'' [p. 55]  Thus these forms are
    supposedly legally evaluable.  Is this what was intended?  
Yes.
							       Should
    evaluation of these forms be or signal an error?  
No.
						      I would certainly
    hope that the answer is the same for both tests.
It is.

    (DEFVAR *TEST-3* '(PROGN (EVAL (BUTLAST *TEST-1*))
			     (DEFUN LEX-TEST ()
				    (LET ((FOO T))
					 (SET-FOO NIL)
					 FOO))
			     (LEX-TEST)))
(eval *test-3*)
==> T
foo
==> NIL

    (DEFVAR *TEST-4* '(PROGN (EVAL (BUTLAST *TEST-3*))
			     (COMPILE 'LEX-TEST)
			     (LEX-TEST)))
(eval *test-4*)
==> T
foo
==> NIL

I believe the "best" alternative is what VAX/NIL does; namely
give a warning like the compiler.  (It would be best if it
did it only once for a given form).  However, this might be
unpopular with users if this warning interferes with their
interaction.

Of course, the real conclusion to draw from this is to
use the compiler.  Most compilers give you a lot of
static error checking that you won't see interpreted.
For example, if your compiler does wrong-number-of-args
checking, it will check the number of arguments even on
parts of your function that are only called once in a blue
moon.