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

object-variable, local variable conflict



   Date: Wed, 6 Feb 91 15:03:15 CST
   From: lynch@aristotle.ils.nwu.edu (Richard Lynch)

   Is there any alternative way of getting the value of an object's
   object-variable directly...Somehow in the macros a symbol is getting bound
   (maybe by a let or as part of a function's lambda-list) to a value, and I
   need to ignore this binding and get my object's binding of its
   object-variable of the same name.  (objvar name) doesn't work.

Just write an obfun which returns the value of the object variable.
Presumably you can muster enough self control to not shadow the object
variable within this obfun.  Then call the obfun from whatever dirty
lexical environment you want, and it'll return the correct value.

Actually, this isn't quite correct.  If you shadow the function (by
creating a lexical function with FLET or LABELS) then once again you
won't be able to access the value.

   I know I should trace this back to its source and rewrite the offending
   macro or function to avoid the conflict, but the path is long and involves
   compiled functions etc that can't be stepped through...

You're right.

You've discovered one of the many pitfalls of programming with macros
in Common Lisp.  In general:

 * A macro should always use gensyms to introduce lexical variables
   in a macro expansion.
 * A macro should be very careful amount making free references, and
   expecting these free references to refer to anything in particular.

For further discussion of these problems, see any of a number of
papers on macros in Scheme.  (For example, "Macros that Work", by
Clinger and Reese.)

   Why in the world do lexical bindings take precedence over object
   bindings anyway?!!!!

Because you have control over whether or not you introduce a lexical
variable with a particular name.  However, you don't necessarily have
control over the names of object variables.

Why would you want to introduce a lexical variable, and then not be
able to reference it (because the object variable is there).  The
notion is that you wouldn't have introduced the lexical variable
unless you wanted to.  If you think about this, you'll see that it
makes sense.

To fix the problems you are having, I'd suggest that you convert your
macros to functions.  If you want to provide a special syntax, you can
write very simple macros which just do some argument shuffling and
expand into a function call.  Using macros to achieve in-line
compilation (as you seem to be doing) is frought with hazards, as
you've discovered.  This is especially true in Object Lisp, which uses
free references to get at instance variables (not the cleanest design
in the world).

   -andrew