| 1 | <html><head> |
| 2 | |
| 3 | <title>Advanced XLISP Objects</title> |
| 4 | |
| 5 | <style type="text/css"> |
| 6 | .example { |
| 7 | color: #000000; |
| 8 | background-color: #F5F5F5; |
| 9 | padding: 8px; |
| 10 | border: #808080; |
| 11 | border-style: solid; |
| 12 | border-width: 1px; |
| 13 | width:auto; |
| 14 | } |
| 15 | .button { |
| 16 | color: #000000; |
| 17 | background-color: #F5F5F5; |
| 18 | padding-top: 1px; |
| 19 | padding-bottom: 1px; |
| 20 | padding-left: 4px; |
| 21 | padding-right: 8px; |
| 22 | border: #808080; |
| 23 | border-style: solid; |
| 24 | border-width: 1px; |
| 25 | white-space: pre; |
| 26 | } |
| 27 | .box { |
| 28 | color: #000000; |
| 29 | padding-top: 4px; |
| 30 | padding-bottom: 4px; |
| 31 | padding-left: 16px; |
| 32 | padding-right: 16px; |
| 33 | border: #808080; |
| 34 | border-style: solid; |
| 35 | border-width: 1px; |
| 36 | } |
| 37 | </style> |
| 38 | |
| 39 | </head> |
| 40 | |
| 41 | <body> |
| 42 | |
| 43 | <a href="../start.htm">Nyquist / XLISP 2.0</a> - |
| 44 | <a href="../manual/contents.htm">Contents</a> | |
| 45 | <a href="../tutorials/tutorials.htm">Tutorials</a> | |
| 46 | <a href="../examples/examples.htm">Examples</a> | |
| 47 | <a href="../reference/reference-index.htm">Reference</a> |
| 48 | |
| 49 | <hr> |
| 50 | |
| 51 | <h1>Advanced XLISP Objects</h1> |
| 52 | |
| 53 | <hr> |
| 54 | |
| 55 | <ol> |
| 56 | <li><nobr><a href="standard-xlisp-objects">Standard XLISP Objects</a></nobr></li> |
| 57 | <li><nobr><a href="initializing-class-variables">Initializing Class Variables</a></nobr></li> |
| 58 | <li><nobr><a href="accessing-class-and-instance-variables">Accessing Class and Instance Variables</a></nobr></li> |
| 59 | </ol> |
| 60 | |
| 61 | <a name="standard-xlisp-objects"></a> |
| 62 | |
| 63 | <hr> |
| 64 | |
| 65 | <h2>Standard XLISP Objects</h2> |
| 66 | |
| 67 | <hr> |
| 68 | |
| 69 | <p>Define a class with an instance variable and a class variable:</p> |
| 70 | |
| 71 | <pre class="example"> |
| 72 | (setq my-class (send class :new '(instance-var) '(class-var))) |
| 73 | </pre> |
| 74 | |
| 75 | <p>Look at the layout of the new class:</p> |
| 76 | |
| 77 | <pre class="example"> |
| 78 | > (send my-class :show) |
| 79 | Object is #<Object...>, Class is #<Object...> |
| 80 | MESSAGES = NIL |
| 81 | IVARS = (INSTANCE-VAR) |
| 82 | CVARS = (CLASS-VAR) |
| 83 | CVALS = #(NIL) <font color="#008844">; default init-value of class variables</font> |
| 84 | SUPERCLASS = #<Object...> |
| 85 | IVARCNT = 1 |
| 86 | IVARTOTAL = 1 |
| 87 | #<Object...> |
| 88 | </pre> |
| 89 | |
| 90 | <p>Make an instance of '<nobr>my-class</nobr>':</p> |
| 91 | |
| 92 | <pre class="example"> |
| 93 | (setq my-object (send my-class :new)) |
| 94 | </pre> |
| 95 | |
| 96 | <p>Look at the layout of the new object:</p> |
| 97 | |
| 98 | <pre class="example"> |
| 99 | > (send my-object :show) |
| 100 | Object is #<Object...>, Class is #<Object...> |
| 101 | INSTANCE-VAR = NIL <font color="#008844">; default init-value of instance variables</font> |
| 102 | #<Object...> |
| 103 | </pre> |
| 104 | |
| 105 | <p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 106 | |
| 107 | <a name=""></a> |
| 108 | |
| 109 | <hr> |
| 110 | |
| 111 | <h2>Initializing Class Variables</h2> |
| 112 | |
| 113 | <hr> |
| 114 | |
| 115 | <p><b>1.</b> Add an :isnew <nobr>init-method</nobr> to '<nobr>my-class</nobr>':</p> |
| 116 | |
| 117 | <pre class="example"> |
| 118 | (send my-class :answer :isnew nil '((setq class-var 1))) |
| 119 | </pre> |
| 120 | |
| 121 | <p>Now reset the class:</p> |
| 122 | |
| 123 | <pre class="example"> |
| 124 | (send my-class :isnew) => <font color="#AA0000">error: too few arguments</font> |
| 125 | </pre> |
| 126 | |
| 127 | <p>It turns out that :isnew needs at least a list of instance variables plus |
| 128 | an optional list of class variables:</p> |
| 129 | |
| 130 | <pre class="example"> |
| 131 | (send my-class :isnew '(ivar)) <font color="#008844">; overwrites INSTANCE-VAR, deletes CLASS-VAR</font> |
| 132 | (send my-class :isnew '(ivar) '(cvar))) <font color="#008844">; overwrites INSTANCE-VAR and CLASS-VAR</font> |
| 133 | </pre> |
| 134 | |
| 135 | <p><b>2.</b> Add an :init method to '<nobr>my-class</nobr>':</p> |
| 136 | |
| 137 | <pre class="example"> |
| 138 | (send my-class :answer :init nil '((setq class-var 1))) |
| 139 | </pre> |
| 140 | |
| 141 | <p>Now call the :init method:</p> |
| 142 | |
| 143 | <pre class="example"> |
| 144 | (send my-class :init) => <font color="#AA0000">error: no method for this message - :INIT</font> |
| 145 | </pre> |
| 146 | |
| 147 | <p>This is not true, there is an :init method:</p> |
| 148 | |
| 149 | <pre class="example"> |
| 150 | > (send my-class :show) |
| 151 | Object is #<Object...>, Class is #<Object...> |
| 152 | MESSAGES = ((<font color="#0000CC">:INIT</font> . <font color="#0000CC">#<Closure-:INIT:...></font>)) |
| 153 | IVARS = (INSTANCE-VAR) |
| 154 | CVARS = (CLASS-VAR) |
| 155 | CVALS = #(NIL) |
| 156 | SUPERCLASS = #<Object...> |
| 157 | IVARCNT = 1 |
| 158 | IVARTOTAL = 1 |
| 159 | #<Object...> |
| 160 | </pre> |
| 161 | |
| 162 | <p>The problem here is that in XLISP, methods cannot be called from the |
| 163 | class they were defined in, methods only can be called from instances, and |
| 164 | exactly the same happens with manipulating class variables. There seems to |
| 165 | be no standard XLISP way to initialize class variables with values at the |
| 166 | time when the class is defined.</p> |
| 167 | |
| 168 | <p><b>3.</b> The only way I know in XLISP to initialize a class variable is |
| 169 | to create an instance of the class and set the class variable e.g. from the |
| 170 | :isnew method of the instance:</p> |
| 171 | |
| 172 | <pre class="example"> |
| 173 | (setq my-object (send my-class :new)) |
| 174 | </pre> |
| 175 | |
| 176 | <p>The :isnew method of '<nobr>my-object</nobr>', inherited from |
| 177 | '<nobr>my-class</nobr>', has set the class variable in |
| 178 | '<nobr>my-class</nobr>' to a new value:</p> |
| 179 | |
| 180 | <pre class="example"> |
| 181 | > (send my-class :show) |
| 182 | Object is #<Object...>, Class is #<Object...> |
| 183 | MESSAGES = ((:ISNEW . #<Closure-:ISNEW:...>)) |
| 184 | IVARS = (INSTANCE-VAR) |
| 185 | CVARS = (CLASS-VAR) |
| 186 | CVALS = #(1) <font color="#008844">; new value of CLASS-VAR</font> |
| 187 | SUPERCLASS = #<Object...> |
| 188 | IVARCNT = 1 |
| 189 | IVARTOTAL = 1 |
| 190 | #<Object...> |
| 191 | </pre> |
| 192 | |
| 193 | <p>This works, but now I have two problems:</p> |
| 194 | |
| 195 | <ol> |
| 196 | |
| 197 | <li><p>If a class variable is set from an instance's :isnew method, |
| 198 | inherited from the superclass, then, whenever an instance is created, the |
| 199 | class variable will be reset to its initial value. <nobr>Note that</nobr> in |
| 200 | Lisp, instances can be created at arbitrary <nobr>run-time</nobr>, not only |
| 201 | at the beginning of a program. Setting class variables from an :isnew method |
| 202 | can produce unexpected <nobr>side-effects</nobr> if a class variable is used |
| 203 | for object <nobr>inter-communication</nobr>.</p></li> |
| 204 | |
| 205 | <li><p>Because instances can be created at arbitrary runtime, a framework |
| 206 | would need to be written when a class variable is allowed to be set or reset |
| 207 | and <nobr>when not</nobr>. <nobr>Ok, if</nobr> class variables are used for |
| 208 | object <nobr>inter-communication</nobr>, a framework needs to be witten |
| 209 | anyway, but I do not want to be forced to think about this only because I |
| 210 | want to initialize a single class variable.</p></li> |
| 211 | |
| 212 | </ol> |
| 213 | |
| 214 | <p><b>4.</b> Here is a trick I use to initialize class variables.</p> |
| 215 | |
| 216 | <p>Create a class with class variables:</p> |
| 217 | |
| 218 | <pre class="example"> |
| 219 | (setq my-class (send class :new nil '(cvar-1 cvar-2))) |
| 220 | </pre> |
| 221 | |
| 222 | <p>Add an :isnew method to set the class variables:</p> |
| 223 | |
| 224 | <pre class="example"> |
| 225 | (send my-class :answer :isnew nil '((setq cvar-1 "a" cvar-2 "b"))) |
| 226 | </pre> |
| 227 | |
| 228 | <p>Create a temporary dummy object to initialize the class variables:</p> |
| 229 | |
| 230 | <pre class="example"> |
| 231 | (let ((x (send my-class :new)))) |
| 232 | </pre> |
| 233 | |
| 234 | <p>Replace the :isnew method with a dummy version <nobr>[or a</nobr> real |
| 235 | version, initializing the instance variables]:</p> |
| 236 | |
| 237 | <pre class="example"> |
| 238 | (send my-class :answer :isnew nil nil) |
| 239 | </pre> |
| 240 | |
| 241 | <p>Now I have a class with initialized class variables:</p> |
| 242 | |
| 243 | <pre class="example"> |
| 244 | > (send my-class :show) |
| 245 | Object is #<Object...>, Class is #<Object...> |
| 246 | MESSAGES = ((:ISNEW . #<Closure-:ISNEW...>)) <font color="#008844">; dummy method</font> |
| 247 | IVARS = NIL |
| 248 | CVARS = (CVAR-1 CVAR-2) <font color="#008844">; class variables</font> |
| 249 | CVALS = #("a" "b") <font color="#008844">; init values</font> |
| 250 | SUPERCLASS = #<Object...> |
| 251 | IVARCNT = 0 |
| 252 | IVARTOTAL = 0 |
| 253 | #<Object...> |
| 254 | </pre> |
| 255 | |
| 256 | <p>See defclass below how to make this work automatically.</p> |
| 257 | |
| 258 | </pre> |
| 259 | |
| 260 | <p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 261 | |
| 262 | <a name=""></a> |
| 263 | |
| 264 | <hr> |
| 265 | |
| 266 | <h2>Accessing Class and Instance Variables</h2> |
| 267 | |
| 268 | <hr> |
| 269 | |
| 270 | <pre class="example"> |
| 271 | (setq my-class (send class :new '(i-var) '(c-var))) |
| 272 | (setq my-object (send my-class :new)) |
| 273 | </pre> |
| 274 | |
| 275 | <p>A message to read internal class and instance variables:</p> |
| 276 | |
| 277 | <pre class="example"> |
| 278 | (send my-class :answer :<font color="#0000CC">internal-slot-get</font> '(slot-name) |
| 279 | '((eval slot-name))) |
| 280 | </pre> |
| 281 | |
| 282 | <p>A message to write internal class and instance variables:</p> |
| 283 | |
| 284 | <pre class="example"> |
| 285 | (send my-class :answer :<font color="#0000CC">internal-slot-set</font> '(slot-name value) |
| 286 | '((eval (list 'setq slot-name value)))) |
| 287 | </pre> |
| 288 | |
| 289 | <p><div class="box"> |
| 290 | |
| 291 | <p><b>Implementation Notes</b></p> |
| 292 | |
| 293 | <p><b>1.</b> It's not really good Lisp style to explicitely call 'eval' in |
| 294 | Lisp code at <nobr>run-time</nobr>, because 'eval' produces a lot of |
| 295 | overhead, but here the only way to get access to the internal environment of |
| 296 | an object is passing the message arguments to 'eval' inside the object |
| 297 | itself.</p> |
| 298 | |
| 299 | <p><b>2.</b> In the XLISP object system, an :answer message can only be |
| 300 | accessed in an <b>instance</b> of a class <nobr>[a sub-class</nobr> or on |
| 301 | object], but not in the class, where the :answer message has been defined, |
| 302 | so the :internal-slot accessor will only work in '<nobr>my-object</nobr>' |
| 303 | but ont in '<nobr>my-class</nobr>'.</p> |
| 304 | |
| 305 | <p><b>3.</b> If a method had been changed in a superclass, the change will |
| 306 | automatically be inherited by all instances of the class |
| 307 | [<nobr>sub-classes</nobr> and objects], with no need to redefine them.</p> |
| 308 | |
| 309 | <p><b>Warning:</b> If '<nobr>internal-slot-set</nobr>' is given a |
| 310 | <nobr>slot-name</nobr> that doesn't exist inside the object, a global |
| 311 | variable will be created.</p> |
| 312 | |
| 313 | </div></p> |
| 314 | |
| 315 | <p>Reading and writing an instance variable:</p> |
| 316 | |
| 317 | <pre class="example"> |
| 318 | > (send my-object :internal-slot-get 'i-var) <font color="#008844">; read</font> |
| 319 | NIL |
| 320 | |
| 321 | > (send my-object :internal-slot-set 'i-var 55) <font color="#008844">; write</font> |
| 322 | 55 |
| 323 | |
| 324 | > (send my-object :internal-slot-get 'i-var) <font color="#008844">; read</font> |
| 325 | 55 |
| 326 | |
| 327 | > (send my-object :show) |
| 328 | Object is #<Object: #9b95998>, Class is #<Object: #9b95c50> |
| 329 | I-VAR = 55 <font color="#008844">; new value</font> |
| 330 | #<Object: #9b95998> |
| 331 | </pre> |
| 332 | |
| 333 | <p>Reading and writing a class variable:</p> |
| 334 | |
| 335 | <pre class="example"> |
| 336 | > (send my-object :internal-slot-get 'c-var) <font color="#008844">; read</font> |
| 337 | NIL |
| 338 | |
| 339 | > (send my-object :internal-slot-set 'c-var 123) <font color="#008844">; write</font> |
| 340 | 123 |
| 341 | |
| 342 | > (send my-object :internal-slot-get 'c-var) <font color="#008844">; read</font> |
| 343 | 123 |
| 344 | |
| 345 | > (send my-class :show) |
| 346 | Object is #<Object: #9b95c50>, Class is #<Object: #9af7dd4> |
| 347 | MESSAGES = ((:INTERNAL-SLOT-GET . #<Closure-:INTERNAL-SLOT-GET: #9b90080>) |
| 348 | (:INTERNAL-SLOT-SET . #<Closure-:INTERNAL-SLOT-SET: #9b900d4>)) |
| 349 | IVARS = (I-VAR) |
| 350 | CVARS = (C-VAR) |
| 351 | CVALS = #(123) <font color="#008844">; new value</font> |
| 352 | SUPERCLASS = #<Object: #9af7dc8> |
| 353 | IVARCNT = 1 |
| 354 | IVARTOTAL = 1 |
| 355 | #<Object: #9b95c50> |
| 356 | </pre> |
| 357 | |
| 358 | <p>See the '<nobr>slot-get</nobr>' and '<nobr>slot-set</nobr>' functions |
| 359 | below how this can be generalized to access any class or instance variable |
| 360 | in any class or object via only two functions.</p> |
| 361 | |
| 362 | <p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 363 | |
| 364 | <a name="defclass"></a> |
| 365 | |
| 366 | <hr> |
| 367 | |
| 368 | <h2>defclass</h2> |
| 369 | |
| 370 | <hr> |
| 371 | |
| 372 | <p>The original RBD 'defclass' macro:</p> |
| 373 | |
| 374 | <pre class="example"> |
| 375 | (defmacro <font color="#0000CC">defclass</font> (name super locals class-vars) |
| 376 | (if (not (boundp name)) |
| 377 | (if super |
| 378 | `(setq ,name (send class :new ',locals ',class-vars ,super)) |
| 379 | `(setq ,name (send class :new ',locals ',class-vars))))) |
| 380 | </pre> |
| 381 | |
| 382 | <p>In order to read or write XLISP class or object variables two |
| 383 | <nobr>slot-acessor</nobr> messages need to be added to every new |
| 384 | <nobr>top-level</nobr> class:</p> |
| 385 | |
| 386 | <pre class="example"> |
| 387 | (defmacro <font color="#0000CC">defclass</font> (name super locals class-vars) |
| 388 | (when (boundp name) |
| 389 | (format t <font color="#880000">";; WARNING: redefining ~a~%"</font> name)) |
| 390 | (if super |
| 391 | `(setq ,name (send class :new ',locals ',class-vars ,super)) |
| 392 | `(progn |
| 393 | (setq ,name (send class :new ',locals ',class-vars)) |
| 394 | (send ,name :answer :internal-slot-set '(slot-name value) |
| 395 | '((eval (list 'setq slot-name value)))) |
| 396 | (send ,name :answer :internal-slot-get '(slot-name) |
| 397 | '((eval slot-name)))))) |
| 398 | </pre> |
| 399 | |
| 400 | <p>The third version provides <nobr>'let'-syntax</nobr> with instance and |
| 401 | class variables. <nobr>A list</nobr> of symbols will create variables |
| 402 | initialized <nobr>to NIL</nobr>. This is the XLISP default behaviour. |
| 403 | <nobr>If an</nobr> element is a <nobr>(symbol value)</nobr> list, then the |
| 404 | variable will be initialized with 'value', as soon as an instance of the |
| 405 | class is created.</p> |
| 406 | |
| 407 | <p><div class="box"> |
| 408 | |
| 409 | <p><table cellpadding="0" cellspacing="0"><tbody> |
| 410 | <tr> |
| 411 | <td colspan="7"><nobr>(<b>defclass</b> <i>class</i> {<i>superclass</i> | NIL}</nobr></td> |
| 412 | </tr> |
| 413 | <tr> |
| 414 | <td><nobr><code> </code>({</nobr></td> |
| 415 | <td align="center"><nobr><i>ivar</i></nobr></td> |
| 416 | <td><nobr> | (</nobr></td> |
| 417 | <td align="center"><nobr><i>ivar</i></nobr></td> |
| 418 | <td><nobr> <i>init-form</i></nobr></td> |
| 419 | <td><nobr>)} ... )<code> </code></nobr></td> |
| 420 | <td><nobr>; instance variables</nobr></td> |
| 421 | </tr> |
| 422 | <tr> |
| 423 | <td><nobr><code> </code>({</nobr></td> |
| 424 | <td align="center"><nobr><i>cvar</i></nobr></td> |
| 425 | <td><nobr> | (</nobr></td> |
| 426 | <td align="center"><nobr><i>cvar</i></nobr></td> |
| 427 | <td><nobr> <i>init-form</i></nobr></td> |
| 428 | <td><nobr>)} ... ))<code> </code></nobr></td> |
| 429 | <td><nobr>; class variables</nobr></td> |
| 430 | </tr> |
| 431 | </tbody></table></p> |
| 432 | |
| 433 | </div></p> |
| 434 | |
| 435 | <pre class="example"> |
| 436 | (defmacro <font color="#0000CC">expand-init-values</font> (vars var-list init-list) |
| 437 | (let ((var (gensym))) |
| 438 | `(dolist (,var ,vars (setq ,var-list (reverse ,var-list) |
| 439 | ,init-list (reverse ,init-list))) |
| 440 | (cond ((symbolp ,var) |
| 441 | <font color="#008844">;; if the element is a single symbol,</font> |
| 442 | <font color="#008844">;; then add it to the variable list</font> |
| 443 | (push ,var ,var-list)) |
| 444 | ((and (listp ,var) (symbolp (first ,var))) |
| 445 | <font color="#008844">;; if the element is a (symbol value) list, then add</font> |
| 446 | <font color="#008844">;; an (setq symbol value) element to the init-list</font> |
| 447 | (push (list 'setq (first ,var) (second ,var)) ,init-list) |
| 448 | <font color="#008844">;; and add the symbol to the variable-list</font> |
| 449 | (push (first ,var) ,var-list)) |
| 450 | (t (error <font color="#880000">"bad argument type"</font> ,var)))))) |
| 451 | |
| 452 | (defmacro <font color="#0000CC">with-unique-names</font> (symbols &rest body) |
| 453 | `(let ,(mapcar #'(lambda (x) `(,x (gensym))) symbols) ,@body)) |
| 454 | |
| 455 | (defmacro <font color="#0000CC">defclass</font> (name super class-vars instance-vars) |
| 456 | (with-unique-names (class-list class-init instance-list instance-init x) |
| 457 | `(let (,instance-list ,instance-init ,class-list ,class-init) |
| 458 | (expand-init-values ',class-vars ,class-list ,class-init) |
| 459 | (expand-init-values ',instance-vars ,instance-list ,instance-init) |
| 460 | (if (boundp ',name) |
| 461 | (format t <font color="#880000">";; Redefining ~a~%"</font> ',name) |
| 462 | (format t <font color="#880000">";; CLASS ~a~%"</font> ',name)) |
| 463 | (format t <font color="#880000">";; CVARS ~a~%"</font> ',class-vars) |
| 464 | (format t <font color="#880000">";; IVARS ~a~%"</font> ',instance-vars) |
| 465 | ,(if super |
| 466 | `(setq ,name (send class :new ,instance-list ,class-list ,super)) |
| 467 | `(setq ,name (send class :new ,instance-list ,class-list))) |
| 468 | <font color="#008844">;; initialize class and instance variables</font> |
| 469 | (when ,class-list |
| 470 | (send ,name :answer :isnew nil ,class-init) |
| 471 | (let ((,x (send ,name :new))))) |
| 472 | (when (or ,instance-list ,class-list) |
| 473 | (send ,name :answer :isnew nil ,instance-init)) |
| 474 | <font color="#008844">;; add slot-accessors to top-level classes</font> |
| 475 | ,(unless super |
| 476 | `(progn |
| 477 | (send ,name :answer :internal-slot-set '(slot-name value) |
| 478 | '((eval (list 'setq slot-name value)))) |
| 479 | (send ,name :answer :internal-slot-get '(slot-name) |
| 480 | '((eval slot-name)))))))) |
| 481 | </pre> |
| 482 | |
| 483 | <p><nobr>Sub-classes</nobr> and objects inherit their acessors from the |
| 484 | <nobr>super-class</nobr>.</p> |
| 485 | |
| 486 | <p>Define a class with an <nobr>instance-variable</nobr>, a |
| 487 | <nobr>class-variable</nobr> and slot acessors:</p> |
| 488 | |
| 489 | <pre class="example"> |
| 490 | > (defclass my-class nil |
| 491 | ((a 1) (b 2) (c 3)) |
| 492 | ((d 4) (e 5) (f 6))) |
| 493 | |
| 494 | > |
| 495 | </pre> |
| 496 | |
| 497 | <p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 498 | |
| 499 | <a name="slot-value"></a> |
| 500 | |
| 501 | <hr> |
| 502 | |
| 503 | <h2>Generalized Slot Accessors</h2> |
| 504 | |
| 505 | <hr> |
| 506 | |
| 507 | <p>Now the slot accessors for internal class and instance variables can be |
| 508 | defined as ordinary XLISP functions:</p> |
| 509 | |
| 510 | <pre class="example"> |
| 511 | (defun <font color="#0000CC">slot-set</font> (object slot-name value) |
| 512 | (send object :internal-slot-set slot-name value)) |
| 513 | |
| 514 | (defun <font color="#0000CC">slot-get</font> (object slot-name) |
| 515 | (send object :internal-slot-get slot-name)) |
| 516 | </pre> |
| 517 | |
| 518 | <p>Examples:</p> |
| 519 | |
| 520 | <pre class="example"> |
| 521 | > (slot-set my-object 'i-var 333) |
| 522 | 333 |
| 523 | |
| 524 | > (slot-get my-object 'i-var) |
| 525 | 333 |
| 526 | </pre> |
| 527 | |
| 528 | <p>Even typing the quote could be saved if 'slot-set' and 'slot-get' are |
| 529 | defined as macros:</p> |
| 530 | |
| 531 | <pre class="example"> |
| 532 | (defmacro <font color="#0000CC">slot-set</font> (object slot-name value) |
| 533 | `(send ,object :internal-slot-set ',slot-name ,value)) |
| 534 | |
| 535 | (defmacro <font color="#0000CC">slot-get</font> (object slot-name) |
| 536 | (send ,object :internal-slot-set ',slot-name ,value)) |
| 537 | </pre> |
| 538 | |
| 539 | <p>Examples:</p> |
| 540 | |
| 541 | <pre class="example"> |
| 542 | > (slot-set my-object i-var 444) |
| 543 | 444 |
| 544 | |
| 545 | > (slot-get my-object i-var) |
| 546 | 444 |
| 547 | </pre> |
| 548 | |
| 549 | <p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 550 | |
| 551 | <a name=""></a> |
| 552 | |
| 553 | <hr> |
| 554 | |
| 555 | <h2>Removing a Method from a Class or Instance</h2> |
| 556 | |
| 557 | <hr> |
| 558 | |
| 559 | <p>In Smalltalk, if a method's body is unbound and no other object refernces |
| 560 | the method, then the method is automatically garbage collected. |
| 561 | Unfortunately in XLISP this doesn't work because the instance variables, |
| 562 | including the list of methods, are not accessed by the garbage collector |
| 563 | <nobr>at all</nobr>. This means that even if the message body is set to NIL, |
| 564 | the message is not garbage collected, instead the '<nobr>no function</nobr>' |
| 565 | message returns NIL and blocks the <nobr>built-in</nobr> search for |
| 566 | <nobr>super-class</nobr> messages.</p> |
| 567 | |
| 568 | <p>Because messages cannot be removed from XLISP objects, the only way to |
| 569 | make a message 'disappear' is to replage it's body by a call to the |
| 570 | <nobr>super-class</nobr>, passing the arguments of the original message:</p> |
| 571 | |
| 572 | <pre class="example"> |
| 573 | (defun remove-method (object message-selector &rest args) |
| 574 | (send object message-selector |
| 575 | (send-super message-selector args)) |
| 576 | </pre> |
| 577 | |
| 578 | <p>Shit: this doesn't work if the metod is defined in a super-class.</p> |
| 579 | |
| 580 | <p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 581 | |
| 582 | <hr> |
| 583 | |
| 584 | <a href="../start.htm">Nyquist / XLISP 2.0</a> - |
| 585 | <a href="../manual/contents.htm">Contents</a> | |
| 586 | <a href="../tutorials/tutorials.htm">Tutorials</a> | |
| 587 | <a href="../examples/examples.htm">Examples</a> | |
| 588 | <a href="../reference/reference-index.htm">Reference</a> |
| 589 | |
| 590 | </body></html> |