1
0
Fork 0
cl-sites/novaspec.org/cl/f_update-instance-for-redefined-class.html
2025-02-05 18:52:26 +01:00

583 lines
No EOL
13 KiB
HTML

<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>update-instance-for-redefined-class | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_update-instance-for-redefined-class.html"
><LINK REL="next" HREF="f_change-class.html" TYPE="text/html" TITLE="change-class"
><LINK REL="prev" HREF="f_update-instance-for-different-class.html" TYPE="text/html" TITLE="update-instance-for-different-class"
><LINK REL="up" HREF="7_7_Objects_Dictionary.html" TYPE="text/html" TITLE="7.7 Objects Dictionary"
><LINK REL="start" HREF="index.html" TYPE="text/html" TITLE="Common Lisp Nova Spec"
><META NAME="VIEWPORT" CONTENT="width=device-width, initial-scale=1.0"
><LINK REL="STYLESHEET" HREF="dpans.css%3F3909942064.css"
><SCRIPT SRC="dpans.js%3F3909942064"
></SCRIPT
><SCRIPT SRC="apropos.js%3F3909942064"
></SCRIPT
></HEAD
><BODY
><DIV
><DIV CLASS="topnav"
><DIV CLASS="breadcrumb"
><SPAN CLASS="breadcrumb-item"
><A HREF="index.html"
>Common Lisp Nova Spec</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="7_Objects.html"
>7. Objects</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="7_7_Objects_Dictionary.html"
>7.7 Objects Dictionary</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="f_update-instance-for-redefined-class.html"
>update-instance-for-redefined-class</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_update-instance-for-different-class.html" CLASS="prev"
>&#8592;</A
><SPAN ID="apropos-label"
>Apropos </SPAN
><INPUT ID="apropos" AUTOFOCUS="AUTOFOCUS" PLACEHOLDER="Type here to search" ONINPUT="aproposInput(this);" ONKEYUP="aproposKeyup(event);" ONCHANGE="aproposChange(this);" ONFOCUS="aproposFocus(this);" ONFOCUSOUT="aproposFocusout(this);"
><A HREF="f_change-class.html" CLASS="next"
>&#8594;</A
></DIV
><DIV ID="apropos-res"
></DIV
></DIV
></DIV
><DIV CLASS="matter"
><DIV CLASS="com"
><DIV CLASS="begincom"
><HR
><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0"
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" WIDTH="100%" CLASS="name"
><SPAN CLASS="idx" DATA-KIND="idxref" DATA-TERM="update-instance-for-redefined-class"
></SPAN
><B
>update-instance-for-redefined-class</B
></TD
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
><I
>Standard</I
> <I
>Generic</I
> <I
>Function</I
></TD
></TR
></TABLE
><HR
></DIV
><UL CLASS="subtoc"
></UL
><DL
><DT
><B
>Syntax</B
></DT
><DD
><TABLE CELLPADDING="0" CELLSPACING="0"
><TR
><TD VALIGN="BASELINE" ALIGN="LEFT" NOWRAP="NOWRAP"
><B
>update-instance-for-redefined-class</B
><SPAN CLASS="cmssi"
>&#160;</SPAN
></TD
><TD VALIGN="BASELINE" ALIGN="LEFT"
><SPAN CLASS="cmssi"
>instance</SPAN
><BR
><SPAN CLASS="cmssi"
>added-slots</SPAN
> <SPAN CLASS="cmssi"
>discarded-slots</SPAN
><BR
><SPAN CLASS="cmssi"
>property-list</SPAN
><BR
><SPAN CLASS="cmtt"
>&amp;rest</SPAN
> <SPAN CLASS="cmssi"
>initargs</SPAN
> <SPAN CLASS="cmtt"
>&amp;key</SPAN
> <SPAN CLASS="cmtt"
>&amp;allow-other-keys</SPAN
></TD
></TR
></TABLE
><DIV
><SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <SPAN CLASS="cmsy"
>{</SPAN
><VAR CLASS="param"
>result</VAR
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*</SPAN
></DIV
></DD
><DT
><B
>Method Signatures</B
></DT
><DD
><TABLE CELLPADDING="0" CELLSPACING="0"
><TR
><TD VALIGN="BASELINE" ALIGN="LEFT" NOWRAP="NOWRAP"
><B
>update-instance-for-redefined-class</B
><SPAN CLASS="cmr"
>&#160;</SPAN
></TD
><TD VALIGN="BASELINE" ALIGN="LEFT"
><SPAN CLASS="cmr"
>(</SPAN
><VAR CLASS="param"
>instance</VAR
> <A HREF="t_standard-object.html" CLASS="typeref"
><B
>standard-object</B
></A
><SPAN CLASS="cmr"
>)</SPAN
><BR
><SPAN CLASS="cmssi"
>added-slots</SPAN
> <SPAN CLASS="cmssi"
>discarded-slots</SPAN
><BR
><SPAN CLASS="cmssi"
>property-list</SPAN
><BR
><SPAN CLASS="cmtt"
>&amp;rest</SPAN
> <SPAN CLASS="cmssi"
>initargs</SPAN
></TD
></TR
></TABLE
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>instance</VAR
> &#8212; an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>added-slots</VAR
> &#8212; a <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>discarded-slots</VAR
> &#8212; a <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>property-list</VAR
> &#8212; a <A HREF="26_1_Glossary.html#list"
><EM CLASS="term"
>list</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>initargs</VAR
> &#8212; an <A HREF="26_1_Glossary.html#initialization_argument_list"
><EM CLASS="term"
>initialization argument list</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>result</VAR
> &#8212; an <A HREF="26_1_Glossary.html#object"
><EM CLASS="term"
>object</EM
></A
>.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
>The <A HREF="26_1_Glossary.html#generic_function"
><EM CLASS="term"
>generic function</EM
></A
> <A HREF="f_update-instance-for-redefined-class.html" CLASS="funref"
><B
>update-instance-for-redefined-class</B
></A
> is not intended to be called by programmers. Programmers may write <A HREF="26_1_Glossary.html#method"
><EM CLASS="term"
>methods</EM
></A
> for it. The <A HREF="26_1_Glossary.html#generic_function"
><EM CLASS="term"
>generic function</EM
></A
> <A HREF="f_update-instance-for-redefined-class.html" CLASS="funref"
><B
>update-instance-for-redefined-class</B
></A
> is called by the mechanism activated by <A HREF="f_make-instances-obsolete.html" CLASS="funref"
><B
>make-instances-obsolete</B
></A
>. </P
><P CLASS="j"
>The system-supplied primary <A HREF="26_1_Glossary.html#method"
><EM CLASS="term"
>method</EM
></A
> on <A HREF="f_update-instance-for-redefined-class.html" CLASS="funref"
><B
>update-instance-for-redefined-class</B
></A
> checks the validity of <VAR CLASS="param"
>initargs</VAR
> and signals an error if an <VAR CLASS="param"
>initarg</VAR
> is supplied that is not declared as valid. This <A HREF="26_1_Glossary.html#method"
><EM CLASS="term"
>method</EM
></A
> then initializes <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slots</EM
></A
> with values according to the <VAR CLASS="param"
>initargs</VAR
>, and initializes the newly <VAR CLASS="param"
>added-slots</VAR
> with values according to their <SPAN CLASS="kwd"
><SPAN CLASS="cmtt"
>:initform</SPAN
></SPAN
> forms. It does this by calling the generic function <A HREF="f_shared-initialize.html" CLASS="funref"
><B
>shared-initialize</B
></A
> with the following arguments: the <VAR CLASS="param"
>instance</VAR
>, a list of names of the newly <VAR CLASS="param"
>added-slots</VAR
> to <VAR CLASS="param"
>instance</VAR
>, and the <VAR CLASS="param"
>initargs</VAR
> it received. Newly <VAR CLASS="param"
>added-slots</VAR
> are those <A HREF="26_1_Glossary.html#local_slot"
><EM CLASS="term"
>local slots</EM
></A
> for which no <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slot</EM
></A
> of the same name exists in the old version of the <A HREF="26_1_Glossary.html#class"
><EM CLASS="term"
>class</EM
></A
>. </P
><P CLASS="j"
>When <A HREF="f_make-instances-obsolete.html" CLASS="funref"
><B
>make-instances-obsolete</B
></A
> is invoked or when a <A HREF="26_1_Glossary.html#class"
><EM CLASS="term"
>class</EM
></A
> has been redefined and an <A HREF="26_1_Glossary.html#instance"
><EM CLASS="term"
>instance</EM
></A
> is being updated, a <VAR CLASS="param"
>property-list</VAR
> is created that captures the slot names and values of all the <VAR CLASS="param"
>discarded-slots</VAR
> with values in the original <VAR CLASS="param"
>instance</VAR
>. The structure of the <VAR CLASS="param"
>instance</VAR
> is transformed so that it conforms to the current class definition. The arguments to <A HREF="f_update-instance-for-redefined-class.html" CLASS="funref"
><B
>update-instance-for-redefined-class</B
></A
> are this transformed <VAR CLASS="param"
>instance</VAR
>, a list of <VAR CLASS="param"
>added-slots</VAR
> to the <VAR CLASS="param"
>instance</VAR
>, a list <VAR CLASS="param"
>discarded-slots</VAR
> from the <VAR CLASS="param"
>instance</VAR
>, and the <VAR CLASS="param"
>property-list</VAR
> containing the slot names and values for <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slots</EM
></A
> that were discarded and had values. Included in this list of discarded <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slots</EM
></A
> are <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slots</EM
></A
> that were local in the old <A HREF="26_1_Glossary.html#class"
><EM CLASS="term"
>class</EM
></A
> and are shared in the new <A HREF="26_1_Glossary.html#class"
><EM CLASS="term"
>class</EM
></A
>. </P
><P CLASS="j"
>The value returned by <A HREF="f_update-instance-for-redefined-class.html" CLASS="funref"
><B
>update-instance-for-redefined-class</B
></A
> is ignored.</P
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
> (defclass position () ())
(defclass x-y-position (position)
((x :initform 0 :accessor position-x)
(y :initform 0 :accessor position-y)))
;;; It turns out polar coordinates are used more than Cartesian
;;; coordinates, so the representation is altered and some new
;;; accessor methods are added.
(defmethod update-instance-for-redefined-class :before
((pos x-y-position) added deleted plist &amp;key)
;; Transform the x-y coordinates to polar coordinates
;; and store into the new slots.
(let ((x (getf plist 'x))
(y (getf plist 'y)))
(setf (position-rho pos) (sqrt (+ (* x x) (* y y)))
(position-theta pos) (atan y x))))
(defclass x-y-position (position)
((rho :initform 0 :accessor position-rho)
(theta :initform 0 :accessor position-theta)))
;;; All instances of the old x-y-position class will be updated
;;; automatically.
;;; The new representation is given the look and feel of the old one.
(defmethod position-x ((pos x-y-position))
(with-slots (rho theta) pos (* rho (cos theta))))
(defmethod (setf position-x) (new-x (pos x-y-position))
(with-slots (rho theta) pos
(let ((y (position-y pos)))
(setq rho (sqrt (+ (* new-x new-x) (* y y)))
theta (atan y new-x))
new-x)))
(defmethod position-y ((pos x-y-position))
(with-slots (rho theta) pos (* rho (sin theta))))
(defmethod (setf position-y) (new-y (pos x-y-position))
(with-slots (rho theta) pos
(let ((x (position-x pos)))
(setq rho (sqrt (+ (* x x) (* new-y new-y)))
theta (atan new-y x))
new-y)))</PRE
></DD
><DT
><B
>Exceptional Situations</B
></DT
><DD
><P CLASS="j"
>The system-supplied primary <A HREF="26_1_Glossary.html#method"
><EM CLASS="term"
>method</EM
></A
> on <A HREF="f_update-instance-for-redefined-class.html" CLASS="funref"
><B
>update-instance-for-redefined-class</B
></A
> signals an error if an <VAR CLASS="param"
>initarg</VAR
> is supplied that is not declared as valid.</P
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="f_make-instances-obsolete.html" CLASS="funref"
><B
>make-instances-obsolete</B
></A
>, <A HREF="f_shared-initialize.html" CLASS="funref"
><B
>shared-initialize</B
></A
>, <A HREF="4_3_Classes.html#sec_4_3_6" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>4.3.6</SPAN
> <SPAN CLASS="cmr"
>(Redefining</SPAN
> <SPAN CLASS="cmr"
>Classes)</SPAN
></A
>, <A HREF="7_1_Object_Creation_and_Initialization.html#sec_7_1_4" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>7.1.4</SPAN
> <SPAN CLASS="cmr"
>(Rules</SPAN
> <SPAN CLASS="cmr"
>for</SPAN
> <SPAN CLASS="cmr"
>Initialization</SPAN
> <SPAN CLASS="cmr"
>Arguments)</SPAN
></A
>, <A HREF="7_1_Object_Creation_and_Initialization.html#sec_7_1_2" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>7.1.2</SPAN
> <SPAN CLASS="cmr"
>(Declaring</SPAN
> <SPAN CLASS="cmr"
>the</SPAN
> <SPAN CLASS="cmr"
>Validity</SPAN
> <SPAN CLASS="cmr"
>of</SPAN
> <SPAN CLASS="cmr"
>Initialization</SPAN
> <SPAN CLASS="cmr"
>Arguments)</SPAN
></A
></P
></DD
><DT
><B
>Notes</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>Initargs</VAR
> are declared as valid by using the <SPAN CLASS="kwd"
><SPAN CLASS="cmtt"
>:initarg</SPAN
></SPAN
> option to <A HREF="f_defclass.html" CLASS="macref"
><B
>defclass</B
></A
>, or by defining <A HREF="26_1_Glossary.html#method"
><EM CLASS="term"
>methods</EM
></A
> for <A HREF="f_update-instance-for-redefined-class.html" CLASS="funref"
><B
>update-instance-for-redefined-class</B
></A
> or <A HREF="f_shared-initialize.html" CLASS="funref"
><B
>shared-initialize</B
></A
>. The keyword name of each keyword parameter specifier in the <A HREF="26_1_Glossary.html#lambda_list"
><EM CLASS="term"
>lambda list</EM
></A
> of any <A HREF="26_1_Glossary.html#method"
><EM CLASS="term"
>method</EM
></A
> defined on <A HREF="f_update-instance-for-redefined-class.html" CLASS="funref"
><B
>update-instance-for-redefined-class</B
></A
> or <A HREF="f_shared-initialize.html" CLASS="funref"
><B
>shared-initialize</B
></A
> is declared as a valid <VAR CLASS="param"
>initarg</VAR
> name for all <A HREF="26_1_Glossary.html#class"
><EM CLASS="term"
>classes</EM
></A
> for which that <A HREF="26_1_Glossary.html#method"
><EM CLASS="term"
>method</EM
></A
> is applicable.</P
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_update-instance-for-different-class.html" CLASS="prev"
>&#8592;</A
><A HREF="f_change-class.html" CLASS="next"
>&#8594;</A
></DIV
><DIV CLASS="trail"
>Conversion to HTML copyright 2023 by Gilbert Baumann</DIV
></DIV
></DIV
><SCRIPT
>domReady();</SCRIPT
></BODY
></HTML
>