1
0
Fork 0
cl-sites/novaspec.org/cl/f_change-class.html

487 lines
11 KiB
HTML
Raw Normal View History

2025-02-05 18:52:26 +01:00
<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>change-class | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_change-class.html"
><LINK REL="next" HREF="f_slot-boundp.html" TYPE="text/html" TITLE="slot-boundp"
><LINK REL="prev" HREF="f_update-instance-for-redefined-class.html" TYPE="text/html" TITLE="update-instance-for-redefined-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_change-class.html"
>change-class</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_update-instance-for-redefined-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_slot-boundp.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="change-class"
></SPAN
><B
>change-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
><P CLASS="j"
><B
>change-class</B
> <SPAN CLASS="cmssi"
>instance</SPAN
> <SPAN CLASS="cmssi"
>new-class</SPAN
> <SPAN CLASS="cmtt"
>&amp;key</SPAN
> <SPAN CLASS="cmtt"
>&amp;allow-other-keys</SPAN
> <SPAN CLASS="arrow"
>&#8594;</SPAN
> <SPAN CLASS="cmssi"
>instance</SPAN
></P
></DD
><DT
><B
>Method Signatures</B
></DT
><DD
><P CLASS="j"
><B
>change-class</B
> (<VAR CLASS="param"
>instance</VAR
> <A HREF="t_standard-object.html" CLASS="typeref"
><B
>standard-object</B
></A
>) (<VAR CLASS="param"
>new-class</VAR
> <A HREF="t_standard-class.html" CLASS="typeref"
><B
>standard-class</B
></A
>) <SPAN CLASS="cmtt"
>&amp;rest</SPAN
> <SPAN CLASS="cmssi"
>initargs</SPAN
></P
><P CLASS="j"
><B
>change-class</B
> (<VAR CLASS="param"
>instance</VAR
> <A HREF="t_t.html" CLASS="typeref"
><B
>t</B
></A
>) (<VAR CLASS="param"
>new-class</VAR
> <A HREF="t_symbol.html" CLASS="typeref"
><B
>symbol</B
></A
>) <SPAN CLASS="cmtt"
>&amp;rest</SPAN
> <SPAN CLASS="cmssi"
>initargs</SPAN
></P
></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"
>new-class</VAR
> &#8212; a <A HREF="26_1_Glossary.html#class_designator"
><EM CLASS="term"
>class designator</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
></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_change-class.html" CLASS="funref"
><B
>change-class</B
></A
> changes the <A HREF="26_1_Glossary.html#class"
><EM CLASS="term"
>class</EM
></A
> of an <VAR CLASS="param"
>instance</VAR
> to <VAR CLASS="param"
>new-class</VAR
>. It destructively modifies and returns the <VAR CLASS="param"
>instance</VAR
>. </P
><P CLASS="j"
>If in the old <A HREF="26_1_Glossary.html#class"
><EM CLASS="term"
>class</EM
></A
> there is any <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slot</EM
></A
> of the same name as a local <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slot</EM
></A
> in the <VAR CLASS="param"
>new-class</VAR
>, the value of that <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slot</EM
></A
> is retained. This means that if the <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slot</EM
></A
> has a value, the value returned by <A HREF="f_slot-value.html" CLASS="funref"
><B
>slot-value</B
></A
> after <A HREF="f_change-class.html" CLASS="funref"
><B
>change-class</B
></A
> is invoked is <A HREF="f_eql.html" CLASS="funref"
><B
>eql</B
></A
> to the value returned by <A HREF="f_slot-value.html" CLASS="funref"
><B
>slot-value</B
></A
> before <A HREF="f_change-class.html" CLASS="funref"
><B
>change-class</B
></A
> is invoked. Similarly, if the <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slot</EM
></A
> was unbound, it remains unbound. The other <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slots</EM
></A
> are initialized as described in <A HREF="7_2_Changing_the_Class_of_an_Instance.html#sec_7_2" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>7.2</SPAN
> <SPAN CLASS="cmr"
>(Changing</SPAN
> <SPAN CLASS="cmr"
>the</SPAN
> <SPAN CLASS="cmr"
>Class</SPAN
> <SPAN CLASS="cmr"
>of</SPAN
> <SPAN CLASS="cmr"
>an</SPAN
> <SPAN CLASS="cmr"
>Instance)</SPAN
></A
>. </P
><P CLASS="j"
>After completing all other actions, <A HREF="f_change-class.html" CLASS="funref"
><B
>change-class</B
></A
> invokes <A HREF="f_update-instance-for-different-class.html" CLASS="funref"
><B
>update-instance-for-different-class</B
></A
>. The generic function <A HREF="f_update-instance-for-different-class.html" CLASS="funref"
><B
>update-instance-for-different-class</B
></A
> can be used to assign values to slots in the transformed instance. See <A HREF="7_2_Changing_the_Class_of_an_Instance.html#sec_7_2_2" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>7.2.2</SPAN
> <SPAN CLASS="cmr"
>(Initializing</SPAN
> <SPAN CLASS="cmr"
>Newly</SPAN
> <SPAN CLASS="cmr"
>Added</SPAN
> <SPAN CLASS="cmr"
>Local</SPAN
> <SPAN CLASS="cmr"
>Slots)</SPAN
></A
>. </P
><P CLASS="j"
>If the second of the above <A HREF="26_1_Glossary.html#method"
><EM CLASS="term"
>methods</EM
></A
> is selected, that <A HREF="26_1_Glossary.html#method"
><EM CLASS="term"
>method</EM
></A
> invokes <A HREF="f_change-class.html" CLASS="funref"
><B
>change-class</B
></A
> on <VAR CLASS="param"
>instance</VAR
>, <CODE CLASS="f"
>(find-class <VAR CLASS="param"
>new-class</VAR
>)</CODE
>, and the <VAR CLASS="param"
>initargs</VAR
>.</P
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
> (defclass position () ())
(defclass x-y-position (position)
((x :initform 0 :initarg :x)
(y :initform 0 :initarg :y)))
(defclass rho-theta-position (position)
((rho :initform 0)
(theta :initform 0)))
(defmethod update-instance-for-different-class :before ((old x-y-position)
(new rho-theta-position)
&amp;key)
;; Copy the position information from old to new to make new
;; be a rho-theta-position at the same position as old.
(let ((x (slot-value old 'x))
(y (slot-value old 'y)))
(setf (slot-value new 'rho) (sqrt (+ (* x x) (* y y)))
(slot-value new 'theta) (atan y x))))
;;; At this point an instance of the class x-y-position can be
;;; changed to be an instance of the class rho-theta-position using
;;; change-class:
(setq p1 (make-instance 'x-y-position :x 2 :y 0))
(change-class p1 'rho-theta-position)
;;; The result is that the instance bound to p1 is now an instance of
;;; the class rho-theta-position. The update-instance-for-different-class
;;; method performed the initialization of the rho and theta slots based
;;; on the value of the x and y slots, which were maintained by
;;; the old instance.</PRE
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="f_update-instance-for-different-class.html" CLASS="funref"
><B
>update-instance-for-different-class</B
></A
>, <A HREF="7_2_Changing_the_Class_of_an_Instance.html#sec_7_2" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>7.2</SPAN
> <SPAN CLASS="cmr"
>(Changing</SPAN
> <SPAN CLASS="cmr"
>the</SPAN
> <SPAN CLASS="cmr"
>Class</SPAN
> <SPAN CLASS="cmr"
>of</SPAN
> <SPAN CLASS="cmr"
>an</SPAN
> <SPAN CLASS="cmr"
>Instance)</SPAN
></A
></P
></DD
><DT
><B
>Notes</B
></DT
><DD
><P CLASS="j"
>The generic function <A HREF="f_change-class.html" CLASS="funref"
><B
>change-class</B
></A
> has several semantic difficulties. First, it performs a destructive operation that can be invoked within a <A HREF="26_1_Glossary.html#method"
><EM CLASS="term"
>method</EM
></A
> on an <A HREF="26_1_Glossary.html#instance"
><EM CLASS="term"
>instance</EM
></A
> that was used to select that <A HREF="26_1_Glossary.html#method"
><EM CLASS="term"
>method</EM
></A
>. When multiple <A HREF="26_1_Glossary.html#method"
><EM CLASS="term"
>methods</EM
></A
> are involved because <A HREF="26_1_Glossary.html#method"
><EM CLASS="term"
>methods</EM
></A
> are being combined, the <A HREF="26_1_Glossary.html#method"
><EM CLASS="term"
>methods</EM
></A
> currently executing or about to be executed may no longer be applicable. Second, some implementations might use compiler optimizations of slot <A HREF="26_1_Glossary.html#access"
><EM CLASS="term"
>access</EM
></A
>, and when the <A HREF="26_1_Glossary.html#class"
><EM CLASS="term"
>class</EM
></A
> of an <A HREF="26_1_Glossary.html#instance"
><EM CLASS="term"
>instance</EM
></A
> is changed the assumptions the compiler made might be violated. This implies that a programmer must not use <A HREF="f_change-class.html" CLASS="funref"
><B
>change-class</B
></A
> inside a <A HREF="26_1_Glossary.html#method"
><EM CLASS="term"
>method</EM
></A
> if any <A HREF="26_1_Glossary.html#method"
><EM CLASS="term"
>methods</EM
></A
> for that <A HREF="26_1_Glossary.html#generic_function"
><EM CLASS="term"
>generic function</EM
></A
> <A HREF="26_1_Glossary.html#access"
><EM CLASS="term"
>access</EM
></A
> any <A HREF="26_1_Glossary.html#slot"
><EM CLASS="term"
>slots</EM
></A
>, or the results are undefined.</P
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_update-instance-for-redefined-class.html" CLASS="prev"
>&#8592;</A
><A HREF="f_slot-boundp.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
>