Understanding Common Lisp Isf Behavior

I do not understand why setf will not work with an array reference returned by a function call. In the example below, why does the final call fail?

 (setf arr #1a(abc)) (defun ret-aref (ai) (aref ai)) (eql (ret-aref arr 0) (aref arr 0)) ;succeeds (progn (setf (aref arr 0) 'foo) arr) ;fails (progn (setf (ret-aref arr 0) 'bar) arr) 
+4
source share
1 answer

The setf statement is actually a macro that should be able to verify the shape of a place already at compile time. He has special knowledge about aref , but knows nothing about your ret-aref .

The easiest way to make your function known to setf is by defining a suitable setf function companion for it. Example:

 (defun (setf ret-aref) (new-value array index) (setf (aref array index) new-value)) 

Now (setf (ret-aref arr 0) 'bar) should work.

This simple example hides the fact that setf extension is actually a pretty hairy topic. You can find gory details at CLHS .

+9
source

All Articles