| 1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
| 2 |
<html> |
| 3 |
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ --> |
| 4 |
<head> |
| 5 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 6 |
<title>change-class (ANSI and GNU Common Lisp Document)</title> |
| 7 |
|
| 8 |
<meta name="description" content="change-class (ANSI and GNU Common Lisp Document)"> |
| 9 |
<meta name="keywords" content="change-class (ANSI and GNU Common Lisp Document)"> |
| 10 |
<meta name="resource-type" content="document"> |
| 11 |
<meta name="distribution" content="global"> |
| 12 |
<meta name="Generator" content="makeinfo"> |
| 13 |
<link href="index.html" rel="start" title="Top"> |
| 14 |
<link href="Objects-Dictionary.html" rel="up" title="Objects Dictionary"> |
| 15 |
<link href="slot_002dboundp.html" rel="next" title="slot-boundp"> |
| 16 |
<link href="update_002dinstance_002dfor_002dredefined_002dclass.html" rel="prev" title="update-instance-for-redefined-class"> |
| 17 |
<style type="text/css"> |
| 18 |
<!-- |
| 19 |
a.summary-letter {text-decoration: none} |
| 20 |
blockquote.indentedblock {margin-right: 0em} |
| 21 |
div.display {margin-left: 3.2em} |
| 22 |
div.example {margin-left: 3.2em} |
| 23 |
div.lisp {margin-left: 3.2em} |
| 24 |
kbd {font-style: oblique} |
| 25 |
pre.display {font-family: inherit} |
| 26 |
pre.format {font-family: inherit} |
| 27 |
pre.menu-comment {font-family: serif} |
| 28 |
pre.menu-preformatted {font-family: serif} |
| 29 |
span.nolinebreak {white-space: nowrap} |
| 30 |
span.roman {font-family: initial; font-weight: normal} |
| 31 |
span.sansserif {font-family: sans-serif; font-weight: normal} |
| 32 |
ul.no-bullet {list-style: none} |
| 33 |
--> |
| 34 |
</style> |
| 35 |
|
| 36 |
|
| 37 |
</head> |
| 38 |
|
| 39 |
<body lang="en"> |
| 40 |
<span id="change_002dclass"></span><div class="header"> |
| 41 |
<p> |
| 42 |
Next: <a href="slot_002dboundp.html" accesskey="n" rel="next">slot-boundp</a>, Previous: <a href="update_002dinstance_002dfor_002dredefined_002dclass.html" accesskey="p" rel="prev">update-instance-for-redefined-class</a>, Up: <a href="Objects-Dictionary.html" accesskey="u" rel="up">Objects Dictionary</a> </p> |
| 43 |
</div> |
| 44 |
<hr> |
| 45 |
<span id="change_002dclass-_005bStandard-Generic-Function_005d"></span><h4 class="subsection">7.7.8 change-class [Standard Generic Function]</h4> |
| 46 |
|
| 47 |
<span id="Syntax_003a_003a-17"></span><h4 class="subsubheading">Syntax::</h4> |
| 48 |
|
| 49 |
<p><code>change-class</code> <i>instance new-class <span class="roman">&key</span> <span class="roman">&allow-other-keys</span></i> ⇒ <i>instance</i> |
| 50 |
</p> |
| 51 |
<span id="Method-Signatures_003a_003a-6"></span><h4 class="subsubheading">Method Signatures::</h4> |
| 52 |
|
| 53 |
<p><code>change-class</code> <i><span class="roman">(</span><i>instance</i> <b>standard-object</b><span class="roman">)</span> |
| 54 |
<span class="roman">(</span><i>new-class</i> <b>standard-class</b><span class="roman">)</span> |
| 55 |
<span class="roman">&rest</span> initargs</i> |
| 56 |
</p> |
| 57 |
<p><code>change-class</code> <i><span class="roman">(</span><i>instance</i> <b>t</b><span class="roman">)</span> |
| 58 |
<span class="roman">(</span><i>new-class</i> <b>symbol</b><span class="roman">)</span> |
| 59 |
<span class="roman">&rest</span> initargs</i> |
| 60 |
</p> |
| 61 |
<span id="Arguments-and-Values_003a_003a-96"></span><h4 class="subsubheading">Arguments and Values::</h4> |
| 62 |
|
| 63 |
<p><i>instance</i>—an <i>object</i>. |
| 64 |
</p> |
| 65 |
<p><i>new-class</i>—a <i>class designator</i>. |
| 66 |
</p> |
| 67 |
<p><i>initargs</i>—an <i>initialization argument list</i>. |
| 68 |
</p> |
| 69 |
<span id="Description_003a_003a-135"></span><h4 class="subsubheading">Description::</h4> |
| 70 |
|
| 71 |
<p>The <i>generic function</i> <b>change-class</b> changes the |
| 72 |
<i>class</i> of an <i>instance</i> to <i>new-class</i>. |
| 73 |
It destructively modifies and returns the <i>instance</i>. |
| 74 |
</p> |
| 75 |
<p>If in the old <i>class</i> there is any <i>slot</i> of the |
| 76 |
same name as a local <i>slot</i> in the <i>new-class</i>, |
| 77 |
the value of that <i>slot</i> is retained. This means that if |
| 78 |
the <i>slot</i> has a value, the value returned by <b>slot-value</b> |
| 79 |
after <b>change-class</b> is invoked is <b>eql</b> to the |
| 80 |
value returned by <b>slot-value</b> before <b>change-class</b> is |
| 81 |
invoked. Similarly, if the <i>slot</i> was unbound, it remains |
| 82 |
unbound. The other <i>slots</i> are initialized as described in |
| 83 |
<a href="Changing-the-Class-of-an-Instance.html">Changing the Class of an Instance</a>. |
| 84 |
</p> |
| 85 |
<p>After completing all other actions, <b>change-class</b> invokes |
| 86 |
<b>update-instance-for-different-class</b>. The |
| 87 |
generic function <b>update-instance-for-different-class</b> can be used |
| 88 |
to assign values to slots in the transformed instance. |
| 89 |
</p> |
| 90 |
<p>See <a href="Initializing-Newly-Added-Local-Slots-_0028Changing-the-Class-of-an-Instance_0029.html">Initializing Newly Added Local Slots (Changing the Class of an Instance)</a>. |
| 91 |
</p> |
| 92 |
<p>If the second of the above <i>methods</i> is selected, |
| 93 |
that <i>method</i> invokes <b>change-class</b> |
| 94 |
on <i>instance</i>, <tt>(find-class <i>new-class</i>)</tt>, |
| 95 |
and the <i>initargs</i>. |
| 96 |
</p> |
| 97 |
<span id="Examples_003a_003a-101"></span><h4 class="subsubheading">Examples::</h4> |
| 98 |
|
| 99 |
<div class="example"> |
| 100 |
<pre class="example"> |
| 101 |
|
| 102 |
(defclass position () ()) |
| 103 |
|
| 104 |
(defclass x-y-position (position) |
| 105 |
((x :initform 0 :initarg :x) |
| 106 |
(y :initform 0 :initarg :y))) |
| 107 |
|
| 108 |
(defclass rho-theta-position (position) |
| 109 |
((rho :initform 0) |
| 110 |
(theta :initform 0))) |
| 111 |
|
| 112 |
(defmethod update-instance-for-different-class :before ((old x-y-position) |
| 113 |
(new rho-theta-position) |
| 114 |
&key) |
| 115 |
;; Copy the position information from old to new to make new |
| 116 |
;; be a rho-theta-position at the same position as old. |
| 117 |
(let ((x (slot-value old 'x)) |
| 118 |
(y (slot-value old 'y))) |
| 119 |
(setf (slot-value new 'rho) (sqrt (+ (* x x) (* y y))) |
| 120 |
(slot-value new 'theta) (atan y x)))) |
| 121 |
|
| 122 |
;;; At this point an instance of the class x-y-position can be |
| 123 |
;;; changed to be an instance of the class rho-theta-position using |
| 124 |
;;; change-class: |
| 125 |
|
| 126 |
(setq p1 (make-instance 'x-y-position :x 2 :y 0)) |
| 127 |
|
| 128 |
(change-class p1 'rho-theta-position) |
| 129 |
|
| 130 |
;;; The result is that the instance bound to p1 is now an instance of |
| 131 |
;;; the class rho-theta-position. The update-instance-for-different-class |
| 132 |
;;; method performed the initialization of the rho and theta slots based |
| 133 |
;;; on the value of the x and y slots, which were maintained by |
| 134 |
;;; the old instance. |
| 135 |
|
| 136 |
</pre></div> |
| 137 |
|
| 138 |
<span id="See-Also_003a_003a-117"></span><h4 class="subsubheading">See Also::</h4> |
| 139 |
|
| 140 |
<p><a href="update_002dinstance_002dfor_002ddifferent_002dclass.html">update-instance-for-different-class</a> |
| 141 |
, |
| 142 |
<a href="Changing-the-Class-of-an-Instance.html">Changing the Class of an Instance</a> |
| 143 |
</p> |
| 144 |
<span id="Notes_003a_003a-79"></span><h4 class="subsubheading">Notes::</h4> |
| 145 |
|
| 146 |
<p>The generic function <b>change-class</b> has several semantic |
| 147 |
difficulties. First, it performs a destructive operation that can be |
| 148 |
invoked within a <i>method</i> on an <i>instance</i> that was used to select that |
| 149 |
<i>method</i>. |
| 150 |
When multiple <i>methods</i> are involved because <i>methods</i> are being |
| 151 |
combined, the <i>methods</i> currently executing or about to be executed may |
| 152 |
no longer be applicable. Second, some implementations might use |
| 153 |
compiler optimizations of slot <i>access</i>, and when the <i>class</i> of an |
| 154 |
<i>instance</i> is changed the assumptions the compiler made might be |
| 155 |
violated. This implies that a programmer must not use |
| 156 |
<b>change-class</b> inside a <i>method</i> if any |
| 157 |
<i>methods</i> for that <i>generic function</i> |
| 158 |
<i>access</i> any <i>slots</i>, or the results are undefined. |
| 159 |
</p> |
| 160 |
<hr> |
| 161 |
<div class="header"> |
| 162 |
<p> |
| 163 |
Next: <a href="slot_002dboundp.html" accesskey="n" rel="next">slot-boundp</a>, Previous: <a href="update_002dinstance_002dfor_002dredefined_002dclass.html" accesskey="p" rel="prev">update-instance-for-redefined-class</a>, Up: <a href="Objects-Dictionary.html" accesskey="u" rel="up">Objects Dictionary</a> </p> |
| 164 |
</div> |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
</body> |
| 169 |
</html> |