Re: using setf with assoc



Jimka wrote:
I noticed that (setf (assoc key alist) newvalue) fails,
and in sbcl I get the following warning:
;   The function (SETF ASSOC) is undefined, and its name is reserved by
ANSI CL so
;   that even if it were defined later, the code doing so would not be
portable.

Does anyone know why this is not implemented?  Is it because noone
could
agree about what it would do?  Or does it really not make sense
in the first place?

It does not make sense:

setf of assoc is not specified in the ANS because there is no way to
specify it that would be consistent with the language conventions.

 - It is required that a setf return the new value stored into the
   <place>.
 - assoc returns a cons of a key and value.  A cons is not reasonably
   defined as a place.  You could, I suppose, have the setf method
   accept a fresh cons, but other code could have previously retrieved
   and cached that cons from the alist, so providing a new cons would
   violate the venerable semantics of alists.  Once a key is consed
   onto an alist, that cons object remains unless explicitly removed
   from the alist.

If you don't understand this you're not quite ready to write setf
methods.
.



Relevant Pages