Auto-creation in a SETF method

JP Massar <[email protected]> Mon, 23 Feb 2004 11:24:29 -0800
Newsgroups gmane.lisp.clump
Message-ID <[email protected]>
Suppose I've defined an class called ZOOP, and it has a slot SLOT.


Now suppose, for whatever perverted reason, I want the following:

(let ((x nil))
   (setf (ZOOP-SLOT x) 23))

to 'just work', that is, if X is not a ZOOP, I want to effectively have the
SETF generate

(SETQ X (MAKE-INSTANCE :ZOOP))

and then do the 'real' SETF using a setf function or whatever.

So

(progn
   (when (not (typep x 'zoop)) (setq x (make-instance :zoop)))
   (setf-zoop-slot x 23))

(For simplicity, for the moment, let's say I only want it to work when
X is a symbol, not an arbitrary place.  And let's ignore completely what
might or might not be the semantics when X is not a place)

After some perusal of DEFINE-SETF-EXPANDER, DEFSETF and whatnot,
I am convincing myself that this is simply not possible.

Any macroexpansion via a setf expander will 'mask' the symbol X by binding
its value to a gensym and using that gensym in place of X
before I can ever get my grubby little hands on it.

Am I correct?  Or is there some esoteric (or not so esoteric) mechanism
using DEFINE-SETF-EXPANDER or DEFSETF that I'm missing?