From: Steve Haflich
Subject: Re: [Q] defining setf method for storing in structure slot
Date: 
Message-ID: <SMH.94Oct26152932@vapor.Franz.COM>
In article <····························@gw.specialform.com> "Keith M. Corbett" <···@specialform.com> writes:

   From: "Keith M. Corbett" <···@specialform.com>

   How do I define a setf method for storing a value in a structure slot with 
   other side effects? To put it another way, how do I call the setf method that 
   is generated by defstruct from within my own setf method?

   Can this even be done with defsetf?

   Specifically I want to (re)set the value for a 2nd slot whenever the 1st is 
   set.

Proabably what's confusing you is that this isn't part of the contract
for the regular setf mechanisms generated by defstruct, and yet that
mechanism is the only portable way set a slot.  That means the name of
the setter function you use externally needs to be different than the
simple one defined by defstruct.

Other than that, a define-setf-method and defsetf can expand into
arbitrary code, so there's little difficulty:

 (defstruct frob aa b)

 (defun frob-a (struct) (frob-aa struct))

 ;; Optional, for efficiency.
 (define-compiler-macro frob-a (struct) `(frob-aa ,struct))

 (defsetf frob-a (struct) (val)
   (let ((s (gensym)))
     `(let ((,s ,struct))
        (setf (frob-b  ,s) nil
              (frob-aa ,s ,val)))))