Debian Codegraph @

docsrc/xlisp/xlisp-doc/manual/objects.htm

raw
1 <html><head><title>XLISP: An Object-oriented Lisp</title></head>
2
3 <link rel="stylesheet" type="text/css" href="manual.css">
4
5 <body>
6
7 <a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
8 <a href="contents.htm">Contents</a> |
9 <a href="../tutorials/tutorials.htm">Tutorials</a> |
10 <a href="../examples/examples.htm">Examples</a> |
11 <a href="../reference/reference-index.htm">Reference</a>
12
13 <hr>
14
15 <h1>XLISP Object System</h1>
16
17 <hr>
18
19 <ol>
20 <li><nobr><a href="#1">Definitions</a></nobr></li>
21 <ul>
22 <li><nobr><a href="#define-selector">selector</a> - a symbol used to select an appropriate method</nobr></li>
23 <li><nobr><a href="#define-message">message</a> - a selector and a list of actual arguments</nobr></li>
24 <li><nobr><a href="#define-method">method</a> - the code that implements a message</nobr></li>
25 <li><nobr><a href="#define-object">object</a> - the top of the class hierarchy</nobr></li>
26 <li><nobr><a href="#define-class">class</a> - the class of all object classes</nobr></li>
27 </ul>
28 <li><nobr><a href="#2">The 'send' Function</a></nobr></li>
29 <li><nobr><a href="#3">The 'self' Symbol</a></nobr></li>
30 <li><nobr><a href="#4">The 'send-super' Function</a></nobr></li>
31 <li><nobr><a href="#5">The 'object' Class</a></nobr></li>
32 <ul>
33 <li><nobr><a href="#object">object</a> - the top of the class hierarchy</nobr></li>
34 <ul>
35 <li><nobr><a href="#object-show">:show</a> - show an object's instance variables</nobr></li>
36 <li><nobr><a href="#object-class">:class</a> - return the class of an object</nobr></li>
37 <li><nobr><a href="#object-isnew">:isnew</a> - the default object initialization routine</nobr></li>
38 <li><nobr><a href="#send-super">send-super</a> - send superclass a message</nobr></li>
39 </ul>
40 </ul>
41 <li><nobr><a href="#6">The 'class' Class</a></nobr></li>
42 <ul>
43 <li><nobr><a href="#class">class</a> - class of all object classes</nobr></li>
44 <ul>
45 <li><nobr><a href="#class-new">:new</a> - create a new instance of a class</nobr></li>
46 <li><nobr><a href="#class-isnew">:isnew</a> - initialize a new class</nobr></li>
47 <li><nobr><a href="#class-answer">:answer</a> - add a message to a class</nobr></li>
48 </ul>
49 </ul>
50 </ol>
51
52 <a name="1"></a>
53 <a name="define-selector"></a>
54 <a name="define-message"></a>
55 <a name="define-method"></a>
56 <a name="define-object"></a>
57 <a name="define-class"></a>
58
59 <hr>
60
61 <h2>1&nbsp; Definitions</h2>
62
63 <hr>
64
65 <p><div class="box">
66
67 <p><table cellpadding="0" cellspacing="0"><tbody>
68 <tr>
69 <td align="right"><nobr>selector</nobr></td>
70 <td valign="top"><nobr>&nbsp;&mdash;&nbsp;</nobr></td>
71 <td width="100%"><nobr>a symbol used to select an appropriate method, usually a keyword</nobr></td>
72 </tr>
73 <tr>
74 <td align="right"><nobr>message</nobr></td>
75 <td valign="top"><nobr>&nbsp;&mdash;&nbsp;</nobr></td>
76 <td width="100%"><nobr>a selector symbol and a list of actual arguments</nobr></td>
77 </tr>
78 <tr>
79 <td align="right"><nobr>method</nobr></td>
80 <td valign="top"><nobr>&nbsp;&mdash;&nbsp;</nobr></td>
81 <td width="100%"><nobr>the Lisp code that implements a message</nobr></td>
82 </tr>
83 <tr>
84 <td align="right"><nobr><a href="../reference/object.htm">object</a></nobr></td>
85 <td valign="top"><nobr>&nbsp;&mdash;&nbsp;</nobr></td>
86 <td width="100%"><nobr>the top of the class hierarchy</nobr></td>
87 </tr>
88 <tr>
89 <td align="right"><nobr><a href="../reference/class.htm">class</a></nobr></td>
90 <td valign="top"><nobr>&nbsp;&mdash;&nbsp;</nobr></td>
91 <td width="100%"><nobr>the class of all object classes [including itself]</nobr></td>
92 </tr>
93 </tbody></table></p>
94
95 </div></p>
96
97 <p>Since XLISP was created to provide a simple basis for experimenting with
98 <nobr>object-oriented</nobr> programming, one of the primitive data types
99 included is <a href="../reference/object.htm">object</a>. <nobr>In
100 XLISP</nobr>, an object consists of a data structure containing a pointer to
101 the object's <a href="../reference/class.htm">class</a> as well as an array
102 containing the values of the object's instance variables.</p>
103
104 <p>Officially, there is no way to see inside an object [look at the values
105 of its instance variables]. The only way to communicate with an object is by
106 sending it a message.</p>
107
108 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to Top</a></nobr></p>
109
110 <a name="2"></a>
111
112 <hr>
113
114 <h2>2&nbsp; The 'send' Function</h2>
115
116 <hr>
117
118 <p>You can send a message to an object using the
119 <a href="../reference/send.htm">send</a> function:</p>
120
121 <p><div class="box">
122
123 <dl>
124 <dt>(<b>send</b> <i>object selector</i> [<i>args</i>])</dt>
125 <dd><i>object</i> - an <a href="object.htm">object</a><br>
126 <i>selector</i> - message selector for <i>object</i><br>
127 <i>arg</i> - parameter sent to <i>object</i> method<br>
128 returns - the <i>object</i></dd>
129 </dl>
130
131 </div></p>
132
133 <p>The <a href="../reference/send.htm">send</a> function takes the object as
134 its first argument, the message selector as its second argument [which must
135 be a symbol] and the message arguments as its remaining arguments. <nobr>It
136 determines</nobr> the class of the receiving object and attempts to find a
137 method corresponding to the message selector in the set of messages defined
138 for that class. <nobr>If the</nobr> message is not found in the object's
139 class and the class has a <nobr>super-class</nobr>, the search continues by
140 looking at the messages defined for the <nobr>super-class</nobr>. This
141 process continues from one <nobr>super-class</nobr> to the next until a
142 method for the message is found. <nobr>If no</nobr> method is found, an
143 error occurs.</p>
144
145 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to Top</a></nobr></p>
146
147 <a name="3"></a>
148
149 <hr>
150
151 <h2>3&nbsp; The 'self' Symbol</h2>
152
153 <hr>
154
155 <p>When a method is found, the evaluator binds the receiving object to the
156 symbol <a href="../reference/self.htm">self</a> and evaluates the method
157 using the remaining elements of the original list as arguments to the
158 method. These arguments are always evaluated prior to being bound to their
159 corresponding formal arguments. The result of evaluating the method becomes
160 the result of the expression.</p>
161
162 <p>Within the body of a method, a message can be sent to the current object
163 by calling:</p>
164
165 <pre class="example">
166 (send self ... )
167 </pre>
168
169 <p>The method lookup starts with the object's class regardless of the class
170 containing the current method.</p>
171
172 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to Top</a></nobr></p>
173
174 <a name="4"></a>
175
176 <hr>
177
178 <h2>4&nbsp; The 'send-super' Function</h2>
179
180 <hr>
181
182 <p>Sometimes it is desirable to invoke a general method in a superclass even
183 when it is overridden by a more specific method in a subclass. This can be
184 accomplished by calling <a
185 href="../reference/send-super.htm">send-super</a>, which begins the
186 method lookup in the superclass of the class defining the current method
187 rather than in the class of the current object:</p>
188
189 <p><div class="box">
190
191 <dl>
192 <dt>(<b>send-super</b> <i>selector</i> [<i>args</i>])</dt>
193 <dd><i>selector</i> - the message selector<br>
194 <i>args</i> - the optional message arguments<br>
195 returns - the result of sending the message</dd>
196 </dl>
197
198 </div></p>
199
200 <p>The <a href="../reference/send-super.htm">send-super</a> function
201 takes a selector as its first argument [which must be a symbol] and the
202 message arguments as its remaining arguments. Notice that <a
203 href="../reference/send-super.htm">send-super</a> can only be sent from
204 within a method, and the target of the message is always the current object
205 <a href="../reference/self.htm">self</a>.</p>
206
207 <pre class="example">
208 (send-super ... )
209 </pre>
210
211 <p>is similar to:</p>
212
213 <pre class="example">
214 (send self ... )
215 </pre>
216
217 <p>except that method lookup begins in the superclass of the class
218 containing the current method rather than the class of the current
219 object.</p>
220
221 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to Top</a></nobr></p>
222
223 <a name="5"></a>
224 <a name="object"></a>
225 <a name="object-show"></a>
226 <a name="object-class"></a>
227 <a name="object-isnew"></a>
228 <a name="send-super"></a>
229
230 <hr>
231
232 <h2>5&nbsp; The 'object' Class</h2>
233
234 <hr>
235
236 <p><div class="box">
237
238 <p><a href="../reference/object.htm">object</a> - the top of the class hierarchy.</p>
239
240 </div></p>
241
242 <p>Messages:</p>
243
244 <p><div class="box">
245
246 <dl>
247
248 <dt>(<a href="../reference/send.htm">send</a> <i>object</i> <a href="../reference/keyword-show.htm">:show</a>)
249 - show an object's instance variables.</dt>
250 <dd>returns - the object</dd>
251
252 <br>
253
254 <dt>(<a href="../reference/send.htm">send</a> <i>object</i> <a href="../reference/keyword-class.htm">:class</a>)
255 - return the class of an object</dt>
256 <dd>returns - the class of the object</dd>
257
258 <br>
259
260 <dt>(<a href="../reference/send.htm">send</a> <i>object</i> <a href="../reference/keyword-isnew.htm">:isnew</a> <i>args</i>)
261 - run the default object initialization routine</dt>
262 <dd>returns - the object</dd>
263
264 <br>
265
266 <dt>(<a href="../reference/send.htm">send</a> <i>object</i> <a href="../reference/keyword-isa.htm">:isa</a> <i>class</i>)
267 - test if <i>object</i> inherits from <i>class</i></dt>
268 <dd>returns - <a href="../reference/t.htm">&nbsp;T&nbsp;</a> if object
269 is an instance of class or a subclass of class, otherwise
270 <a href="../reference/nil.htm">NIL</a></dd>
271
272 </dl>
273
274 </div></p>
275
276 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to Top</a></nobr></p>
277
278 <a name="6"></a>
279 <a name="class"></a>
280 <a name="class-new"></a>
281 <a name="class-isnew"></a>
282 <a name="class-answer"></a>
283
284 <hr>
285
286 <h2>6&nbsp; The 'class' Class</h2>
287
288 <hr>
289
290 <p><div class="box">
291
292 <p><a href="../reference/class.htm">class</a>
293 - class of all object classes (including itself)</p>
294
295 </div></p>
296
297 <p>Messages:</p>
298
299 <p><div class="box">
300
301 <dl>
302
303 <dt>(<a href="../reference/send.htm">send</a> <i>class</i> <a href="../reference/keyword-new.htm">:new</a> <i>ivars</i> [<i>cvars</i> [<i>super</i>]])
304 - create a new instance of a class</dt>
305 <dd>returns - the new class object</dd>
306
307 <br>
308
309 <dt>(<a href="../reference/send.htm">send</a> <i>class</i> <a href="../reference/keyword-isnew.htm">:isnew</a> <i>ivars</i> [<i>cvars</i> [<i>super</i>]])
310 - initialize a new class</dt>
311 <dd><i>ivars</i> - list of instance variable symbols<br>
312 <i>cvars</i> - list of class variable symbols<br>
313 <i>super</i> - the superclass, default is <a href="../reference/object.htm">object</a><br>
314 returns - the new class object</dd>
315
316 <br>
317
318 <dt>(<a href="../reference/send.htm">send</a> <i>class</i> <a href="../reference/keyword-answer.htm">:answer</a> <i>selector</i> <i>fargs</i> <i>body</i>)
319 - add a message to a class</dt>
320 <dd><i>selector</i> - a message selector symbol<br>
321 <i>fargs</i> - the formal argument list, a lambda list<br>
322 <i>body</i> - a list of executable expressions<br>
323 returns - the object</dd>
324
325 </dl>
326
327 </div></p>
328
329 <p>When a new instance of a
330 <a href="../reference/class.htm">class</a> is created by sending
331 the message <a href="../reference/keyword-new.htm">:new</a>
332 to an existing <a href="../reference/class.htm">class</a>, the
333 message <a href="../reference/keyword-isnew.htm">:isnew</a> followed by
334 whatever parameters were passed to the
335 <a href="../reference/keyword-new.htm">:new</a> message is sent to the
336 newly created <a href="../reference/object.htm">object</a>.</p>
337
338 <p>When a new <a href="../reference/class.htm">class</a> is created
339 by sending the <a href="../reference/keyword-new.htm">:new</a> message to
340 the object <a href="../reference/class.htm">class</a>, an optional
341 parameter may be specified indicating the superclass of the new
342 <a href="../reference/class.htm">class</a>. If this parameter is
343 omitted, the new <a href="../reference/class.htm">class</a> will be
344 a subclass of <a href="../reference/object.htm">object</a>. A
345 <a href="../reference/class.htm">class</a> inherits all instance
346 variables, class variables, and methods from its superclass.</p>
347
348 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to Top</a></nobr></p>
349
350 <hr>
351
352 <a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
353 <a href="contents.htm">Contents</a> |
354 <a href="../tutorials/tutorials.htm">Tutorials</a> |
355 <a href="../examples/examples.htm">Examples</a> |
356 <a href="../reference/reference-index.htm">Reference</a>
357
358 </body></html>