Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!lll-winken!uwm.edu!rpi!sci.ccny.cuny.edu!phri!marob!cowan From: cowan@marob.masa.com (John Cowan) Newsgroups: comp.lang.lisp Subject: Re: updating random slots in (say) defstruct Message-ID: <25B37814.3312@marob.masa.com> Date: 16 Jan 90 19:37:54 GMT References: <18837@bellcore.bellcore.com> Reply-To: cowan@marob.masa.com (John Cowan) Organization: ESCC, New York City Lines: 31 In article <18837@bellcore.bellcore.com> pc@flash.bellcore.com writes: [defstruct deleted] >There appears to be no Common LISP way to similarly update a random >slot [of a structure]. The best one can do seems to be: [example using 'case' deleted] >Must i explicitly define my structure as say a list, to be able to >replace an arbitrary component? Symbolics documentation on LOCFs is a >little sketchy, but perhaps i can use them? > >Ideas? This is handled on page 96 of CLtL (1st ed.), which discusses setf. That is section 7.2, (setf), seventh bullet, in case paginations differ. It is legal to say (setf (apply #'fn args) newval). In this case, one could say something like: (setf instance (make-fubar :one 'a :two 'b :three 'c)) (setf accessor #'fubar-one) ; note use of #' here (setf (apply accessor (list instance)) newvalue) to change the :one slot of instance to newvalue. The book warns that this works only if the setf method for the function is of a normal kind, i.e. produces an update function that resembles the access function except for having a final argument added which is the new value. So the solution is somewhat implementation dependent, but should work on any "reasonable" implementation. (I have not tested it past one implementation.)