I don’t understand why setf will not work with an array reference returned from a function call. In the below example, why does the final call fail?
(setf arr #1a(a b c))
(defun ret-aref (a i)
(aref a i))
(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)
The
setfoperator is actually a macro, which needs to be able to inspect the place-form already at compile time. It has special knowledge aboutaref, but doesn’t know anything about yourret-aref.The easiest way to make your function known to
setfis by defining a suitablesetf-function companion for it. Example:Now,
(setf (ret-aref arr 0) 'bar)should work.This simple example hides the fact, that
setf-expansion is actually a pretty hairy topic. You can find the gory details in the CLHS.