About "setf" in "defun"?

Actually, I don’t quite understand the use of setf in defun:

(defun cookies-out* (&optional (reply *reply*)) "Returns an alist of the outgoing cookies associated with the REPLY object REPLY." (cookies-out reply)) (defun (setf cookies-out*) (new-value &optional (reply *reply*)) "Sets the alist of the outgoing cookies associated with the REPLY object REPLY." (setf (cookies-out reply) new-value)) 

I suppose it overrides the # 'cook-out * function, doesn't it? If so, then #'cookies-out* will be a polymorphic function.

Respectfully!

+6
source share
1 answer

Your code snippet does not override any functions. This, in essence, is an “extension” of the possibilities of assigning Common Lisp - one of the nice features of the language. If someone tries (setf (cookies-out * ...) ...), then your last piece of code will be used to complete this task.

The commentary on your question has already been mentioned about these "setf expanders", and in order to dwell on this in detail, you may find it useful to read the following elements from the CLHS. Note that your example uses the form (defun (setf ...) ...), and therefore the first two links apply.

DEFUN allows you to define the function that will be run for SETF.

DEFSETF allows you to name a function (in short form) for the job or to define a macro (in long form) that will be extended to SETF, and you usually need to be careful to use PROGN correctly for the latter case.

DEFINE-SETF-EXPANDER is used where the long DEFSETF form is not enough, although I never had a reason to use it. See this comp.lang.lisp stream for some useful information about this: https://groups.google.com/forum/#!searchin/comp.lang.lisp/difference $ 20between $ 20defsetf $ 20defun $ 20setf / comp .lang.lisp / 5x3fjzMQ6Q0 / _y8Q-p-SVPQJ

My personal experience was that the need to write my own SETF extenders is rare, given that I write more and more of my code in a “functional” style. However, when reaching complex structures for making changes, it can often prove to be a very convenient method ... at least until I understand if functional lenses are a suitable / desirable alternative.

+14
source

Source: https://habr.com/ru/post/905681/


All Articles