| 1 |
'\" |
| 2 |
'\" Copyright (c) 2007 Donal K. Fellows |
| 3 |
'\" |
| 4 |
'\" See the file "license.terms" for information on usage and redistribution |
| 5 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 6 |
'\" |
| 7 |
.TH define n 0.3 TclOO "TclOO Commands" |
| 8 |
.so man.macros |
| 9 |
.BS |
| 10 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 11 |
.SH NAME |
| 12 |
oo::define, oo::objdefine, oo::Slot \- define and configure classes and objects |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
package require TclOO |
| 16 |
|
| 17 |
\fBoo::define\fI class defScript\fR |
| 18 |
\fBoo::define\fI class subcommand arg\fR ?\fIarg ...\fR? |
| 19 |
\fBoo::objdefine\fI object defScript\fR |
| 20 |
\fBoo::objdefine\fI object subcommand arg\fR ?\fIarg ...\fR? |
| 21 |
|
| 22 |
\fBoo::Slot\fR \fIarg...\fR |
| 23 |
.fi |
| 24 |
.SH "CLASS HIERARCHY" |
| 25 |
.nf |
| 26 |
\fBoo::object\fR |
| 27 |
\(-> \fBoo::Slot\fR |
| 28 |
.fi |
| 29 |
.BE |
| 30 |
.SH DESCRIPTION |
| 31 |
The \fBoo::define\fR command is used to control the configuration of classes, |
| 32 |
and the \fBoo::objdefine\fR command is used to control the configuration of |
| 33 |
objects (including classes as instance objects), with the configuration being |
| 34 |
applied to the entity named in the \fIclass\fR or the \fIobject\fR argument. |
| 35 |
Configuring a class also updates the |
| 36 |
configuration of all subclasses of the class and all objects that are |
| 37 |
instances of that class or which mix it in (as modified by any per-instance |
| 38 |
configuration). The way in which the configuration is done is controlled by |
| 39 |
either the \fIdefScript\fR argument or by the \fIsubcommand\fR and following |
| 40 |
\fIarg\fR arguments; when the second is present, it is exactly as if all the |
| 41 |
arguments from \fIsubcommand\fR onwards are made into a list and that list is |
| 42 |
used as the \fIdefScript\fR argument. |
| 43 |
.SS "CONFIGURING CLASSES" |
| 44 |
.PP |
| 45 |
The following commands are supported in the \fIdefScript\fR for |
| 46 |
\fBoo::define\fR, each of which may also be used in the \fIsubcommand\fR form: |
| 47 |
.TP |
| 48 |
\fBconstructor\fI argList bodyScript\fR |
| 49 |
. |
| 50 |
This creates or updates the constructor for a class. The formal arguments to |
| 51 |
the constructor (defined using the same format as for the Tcl \fBproc\fR |
| 52 |
command) will be \fIargList\fR, and the body of the constructor will be |
| 53 |
\fIbodyScript\fR. When the body of the constructor is evaluated, the current |
| 54 |
namespace of the constructor will be a namespace that is unique to the object |
| 55 |
being constructed. Within the constructor, the \fBnext\fR command should be |
| 56 |
used to call the superclasses' constructors. If \fIbodyScript\fR is the empty |
| 57 |
string, the constructor will be deleted. |
| 58 |
.TP |
| 59 |
\fBdeletemethod\fI name\fR ?\fIname ...\fR? |
| 60 |
. |
| 61 |
This deletes each of the methods called \fIname\fR from a class. The methods |
| 62 |
must have previously existed in that class. Does not affect the superclasses |
| 63 |
of the class, nor does it affect the subclasses or instances of the class |
| 64 |
(except when they have a call chain through the class being modified) or the |
| 65 |
class object itself. |
| 66 |
.TP |
| 67 |
\fBdestructor\fI bodyScript\fR |
| 68 |
. |
| 69 |
This creates or updates the destructor for a class. Destructors take no |
| 70 |
arguments, and the body of the destructor will be \fIbodyScript\fR. The |
| 71 |
destructor is called when objects of the class are deleted, and when called |
| 72 |
will have the object's unique namespace as the current namespace. Destructors |
| 73 |
should use the \fBnext\fR command to call the superclasses' destructors. Note |
| 74 |
that destructors are not called in all situations (e.g. if the interpreter is |
| 75 |
destroyed). If \fIbodyScript\fR is the empty string, the destructor will be |
| 76 |
deleted. |
| 77 |
.RS |
| 78 |
Note that errors during the evaluation of a destructor \fIare not returned\fR |
| 79 |
to the code that causes the destruction of an object. Instead, they are passed |
| 80 |
to the currently-defined \fBbgerror\fR handler. |
| 81 |
.RE |
| 82 |
.TP |
| 83 |
\fBexport\fI name \fR?\fIname ...\fR? |
| 84 |
. |
| 85 |
This arranges for each of the named methods, \fIname\fR, to be exported |
| 86 |
(i.e. usable outside an instance through the instance object's command) by the |
| 87 |
class being defined. Note that the methods themselves may be actually defined |
| 88 |
by a superclass; subclass exports override superclass visibility, and may in |
| 89 |
turn be overridden by instances. |
| 90 |
.TP |
| 91 |
\fBfilter\fR ?\fI\-slotOperation\fR? ?\fImethodName ...\fR? |
| 92 |
.VS |
| 93 |
This slot (see \fBSLOTTED DEFINITIONS\fR below) |
| 94 |
.VE |
| 95 |
sets or updates the list of method names that are used to guard whether |
| 96 |
method call to instances of the class may be called and what the method's |
| 97 |
results are. Each \fImethodName\fR names a single filtering method (which may |
| 98 |
be exposed or not exposed); it is not an error for a non-existent method to be |
| 99 |
named since they may be defined by subclasses. |
| 100 |
.VS |
| 101 |
By default, this slot works by appending. |
| 102 |
.VE |
| 103 |
.TP |
| 104 |
\fBforward\fI name cmdName \fR?\fIarg ...\fR? |
| 105 |
. |
| 106 |
This creates or updates a forwarded method called \fIname\fR. The method is |
| 107 |
defined be forwarded to the command called \fIcmdName\fR, with additional |
| 108 |
arguments, \fIarg\fR etc., added before those arguments specified by the |
| 109 |
caller of the method. The \fIcmdName\fR will always be resolved using the |
| 110 |
rules of the invoking objects' namespaces, i.e., when \fIcmdName\fR is not |
| 111 |
fully-qualified, the command will be searched for in each object's namespace, |
| 112 |
using the instances' namespace's path, or by looking in the global namespace. |
| 113 |
The method will be exported if \fIname\fR starts with a lower-case letter, and |
| 114 |
non-exported otherwise. |
| 115 |
.TP |
| 116 |
\fBmethod\fI name argList bodyScript\fR |
| 117 |
. |
| 118 |
This creates or updates a method that is implemented as a procedure-like |
| 119 |
script. The name of the method is \fIname\fR, the formal arguments to the |
| 120 |
method (defined using the same format as for the Tcl \fBproc\fR command) will |
| 121 |
be \fIargList\fR, and the body of the method will be \fIbodyScript\fR. When |
| 122 |
the body of the method is evaluated, the current namespace of the method will |
| 123 |
be a namespace that is unique to the current object. The method will be |
| 124 |
exported if \fIname\fR starts with a lower-case letter, and non-exported |
| 125 |
otherwise; this behavior can be overridden via \fBexport\fR and |
| 126 |
\fBunexport\fR. |
| 127 |
.TP |
| 128 |
\fBmixin\fR ?\fI\-slotOperation\fR? ?\fIclassName ...\fR? |
| 129 |
.VS |
| 130 |
This slot (see \fBSLOTTED DEFINITIONS\fR below) |
| 131 |
.VE |
| 132 |
sets or updates the list of additional classes that are to be mixed into |
| 133 |
all the instances of the class being defined. Each \fIclassName\fR argument |
| 134 |
names a single class that is to be mixed in. |
| 135 |
.VS |
| 136 |
By default, this slot works by replacement. |
| 137 |
.VE |
| 138 |
.TP |
| 139 |
\fBrenamemethod\fI fromName toName\fR |
| 140 |
. |
| 141 |
This renames the method called \fIfromName\fR in a class to \fItoName\fR. The |
| 142 |
method must have previously existed in the class, and \fItoName\fR must not |
| 143 |
previously refer to a method in that class. Does not affect the superclasses |
| 144 |
of the class, nor does it affect the subclasses or instances of the class |
| 145 |
(except when they have a call chain through the class being modified), or the |
| 146 |
class object itself. Does |
| 147 |
not change the export status of the method; if it was exported before, it will |
| 148 |
be afterwards. |
| 149 |
.TP |
| 150 |
\fBself\fI subcommand arg ...\fR |
| 151 |
.TP |
| 152 |
\fBself\fI script\fR |
| 153 |
. |
| 154 |
This command is equivalent to calling \fBoo::objdefine\fR on the class being |
| 155 |
defined (see \fBCONFIGURING OBJECTS\fR below for a description of the |
| 156 |
supported values of \fIsubcommand\fR). It follows the same general pattern of |
| 157 |
argument handling as the \fBoo::define\fR and \fBoo::objdefine\fR commands, |
| 158 |
and |
| 159 |
.QW "\fBoo::define \fIcls \fBself \fIsubcommand ...\fR" |
| 160 |
operates identically to |
| 161 |
.QW "\fBoo::objdefine \fIcls subcommand ...\fR" . |
| 162 |
.TP |
| 163 |
\fBsuperclass\fR ?\fI\-slotOperation\fR? ?\fIclassName ...\fR? |
| 164 |
.VS |
| 165 |
This slot (see \fBSLOTTED DEFINITIONS\fR below) |
| 166 |
.VE |
| 167 |
allows the alteration of the superclasses of the class being defined. |
| 168 |
Each \fIclassName\fR argument names one class that is to be a superclass of |
| 169 |
the defined class. Note that objects must not be changed from being classes to |
| 170 |
being non-classes or vice-versa, that an empty parent class is equivalent to |
| 171 |
\fBoo::object\fR, and that the parent classes of \fBoo::object\fR and |
| 172 |
\fBoo::class\fR may not be modified. |
| 173 |
.VS |
| 174 |
By default, this slot works by replacement. |
| 175 |
.VE |
| 176 |
.TP |
| 177 |
\fBunexport\fI name \fR?\fIname ...\fR? |
| 178 |
. |
| 179 |
This arranges for each of the named methods, \fIname\fR, to be not exported |
| 180 |
(i.e. not usable outside the instance through the instance object's command, |
| 181 |
but instead just through the \fBmy\fR command visible in each object's |
| 182 |
context) by the class being defined. Note that the methods themselves may be |
| 183 |
actually defined by a superclass; subclass unexports override superclass |
| 184 |
visibility, and may be overridden by instance unexports. |
| 185 |
.TP |
| 186 |
\fBvariable\fR ?\fI\-slotOperation\fR? ?\fIname ...\fR? |
| 187 |
.VS |
| 188 |
This slot (see \fBSLOTTED DEFINITIONS\fR below) arranges for each of the named |
| 189 |
variables to be automatically made |
| 190 |
available in the methods, constructor and destructor declared by the class |
| 191 |
being defined. Each variable name must not have any namespace |
| 192 |
separators and must not look like an array access. All variables will be |
| 193 |
actually present in the instance object on which the method is executed. Note |
| 194 |
that the variable lists declared by a superclass or subclass are completely |
| 195 |
disjoint, as are variable lists declared by instances; the list of variable |
| 196 |
names is just for methods (and constructors and destructors) declared by this |
| 197 |
class. By default, this slot works by appending. |
| 198 |
.VE |
| 199 |
.SS "CONFIGURING OBJECTS" |
| 200 |
.PP |
| 201 |
The following commands are supported in the \fIdefScript\fR for |
| 202 |
\fBoo::objdefine\fR, each of which may also be used in the \fIsubcommand\fR |
| 203 |
form: |
| 204 |
.TP |
| 205 |
\fBclass\fI className\fR |
| 206 |
. |
| 207 |
This allows the class of an object to be changed after creation. Note that the |
| 208 |
class's constructors are not called when this is done, and so the object may |
| 209 |
well be in an inconsistent state unless additional configuration work is done. |
| 210 |
.TP |
| 211 |
\fBdeletemethod\fI name\fR ?\fIname ...\fR |
| 212 |
. |
| 213 |
This deletes each of the methods called \fIname\fR from an object. The methods |
| 214 |
must have previously existed in that object (e.g., because it was created |
| 215 |
through \fBoo::objdefine method\fR). Does not affect the classes that the |
| 216 |
object is an instance of, or remove the exposure of those class-provided |
| 217 |
methods in the instance of that class. |
| 218 |
.TP |
| 219 |
\fBexport\fI name \fR?\fIname ...\fR? |
| 220 |
. |
| 221 |
This arranges for each of the named methods, \fIname\fR, to be exported |
| 222 |
(i.e. usable outside the object through the object's command) by the object |
| 223 |
being defined. Note that the methods themselves may be actually defined by a |
| 224 |
class or superclass; object exports override class visibility. |
| 225 |
.TP |
| 226 |
\fBfilter\fR ?\fI\-slotOperation\fR? ?\fImethodName ...\fR? |
| 227 |
.VS |
| 228 |
This slot (see \fBSLOTTED DEFINITIONS\fR below) |
| 229 |
.VE |
| 230 |
sets or updates the list of method names that are used to guard whether a |
| 231 |
method call to the object may be called and what the method's results are. |
| 232 |
Each \fImethodName\fR names a single filtering method (which may be exposed or |
| 233 |
not exposed); it is not an error for a non-existent method to be named. Note |
| 234 |
that the actual list of filters also depends on the filters set upon any |
| 235 |
classes that the object is an instance of. |
| 236 |
.VS |
| 237 |
By default, this slot works by appending. |
| 238 |
.VE |
| 239 |
.TP |
| 240 |
\fBforward\fI name cmdName \fR?\fIarg ...\fR? |
| 241 |
. |
| 242 |
This creates or updates a forwarded object method called \fIname\fR. The |
| 243 |
method is defined be forwarded to the command called \fIcmdName\fR, with |
| 244 |
additional arguments, \fIarg\fR etc., added before those arguments specified |
| 245 |
by the caller of the method. Forwarded methods should be deleted using the |
| 246 |
\fBmethod\fR subcommand. The method will be exported if \fIname\fR starts with |
| 247 |
a lower-case letter, and non-exported otherwise. |
| 248 |
.TP |
| 249 |
\fBmethod\fI name argList bodyScript\fR |
| 250 |
. |
| 251 |
This creates, updates or deletes an object method. The name of the method is |
| 252 |
\fIname\fR, the formal arguments to the method (defined using the same format |
| 253 |
as for the Tcl \fBproc\fR command) will be \fIargList\fR, and the body of the |
| 254 |
method will be \fIbodyScript\fR. When the body of the method is evaluated, the |
| 255 |
current namespace of the method will be a namespace that is unique to the |
| 256 |
object. The method will be exported if \fIname\fR starts with a lower-case |
| 257 |
letter, and non-exported otherwise. |
| 258 |
.TP |
| 259 |
\fBmixin\fR ?\fI\-slotOperation\fR? ?\fIclassName ...\fR? |
| 260 |
.VS |
| 261 |
This slot (see \fBSLOTTED DEFINITIONS\fR below) |
| 262 |
.VE |
| 263 |
sets or updates a per-object list of additional classes that are to be |
| 264 |
mixed into the object. Each argument, \fIclassName\fR, names a single class |
| 265 |
that is to be mixed in. |
| 266 |
.VS |
| 267 |
By default, this slot works by replacement. |
| 268 |
.VE |
| 269 |
.TP |
| 270 |
\fBrenamemethod\fI fromName toName\fR |
| 271 |
. |
| 272 |
This renames the method called \fIfromName\fR in an object to \fItoName\fR. |
| 273 |
The method must have previously existed in the object, and \fItoName\fR must |
| 274 |
not previously refer to a method in that object. Does not affect the classes |
| 275 |
that the object is an instance of and cannot rename in an instance object the |
| 276 |
methods provided by those classes (though a \fBoo::objdefine forward\fRed |
| 277 |
method may provide an equivalent capability). Does not change the export |
| 278 |
status of the method; if it was exported before, it will be afterwards. |
| 279 |
.TP |
| 280 |
\fBunexport\fI name \fR?\fIname ...\fR? |
| 281 |
. |
| 282 |
This arranges for each of the named methods, \fIname\fR, to be not exported |
| 283 |
(i.e. not usable outside the object through the object's command, but instead |
| 284 |
just through the \fBmy\fR command visible in the object's context) by the |
| 285 |
object being defined. Note that the methods themselves may be actually defined |
| 286 |
by a class; instance unexports override class visibility. |
| 287 |
.TP |
| 288 |
\fBvariable\fR ?\fI\-slotOperation\fR? ?\fIname ...\fR? |
| 289 |
.VS |
| 290 |
This slot (see \fBSLOTTED DEFINITIONS\fR below) arranges for each of the named |
| 291 |
variables to be automatically made available in the methods declared by the |
| 292 |
object being defined. Each variable name must not have any namespace |
| 293 |
separators and must not look like an array access. All variables will be |
| 294 |
actually present in the object on which the method is executed. Note that the |
| 295 |
variable lists declared by the classes and mixins of which the object is an |
| 296 |
instance are completely disjoint; the list of variable names is just for |
| 297 |
methods declared by this object. By default, this slot works by appending. |
| 298 |
.SH "SLOTTED DEFINITIONS" |
| 299 |
Some of the configurable definitions of a class or object are \fIslotted |
| 300 |
definitions\fR. This means that the configuration is implemented by a slot |
| 301 |
object, that is an instance of the class \fBoo::Slot\fR, which manages a list |
| 302 |
of values (class names, variable names, etc.) that comprises the contents of |
| 303 |
the slot. |
| 304 |
.PP |
| 305 |
The \fBoo::Slot\fR class defines three operations (as methods) that may be done |
| 306 |
on the slot: |
| 307 |
.VE |
| 308 |
.TP |
| 309 |
\fIslot\fR \fB\-append\fR ?\fImember ...\fR? |
| 310 |
.VS |
| 311 |
This appends the given \fImember\fR elements to the slot definition. |
| 312 |
.VE |
| 313 |
.TP |
| 314 |
\fIslot\fR \fB\-clear\fR |
| 315 |
.VS |
| 316 |
This sets the slot definition to the empty list. |
| 317 |
.VE |
| 318 |
.TP |
| 319 |
\fIslot\fR \fB\-set\fR ?\fImember ...\fR? |
| 320 |
.VS |
| 321 |
This replaces the slot definition with the given \fImember\fR elements. |
| 322 |
.PP |
| 323 |
A consequence of this is that any use of a slot's default operation where the |
| 324 |
first member argument begins with a hyphen will be an error. One of the above |
| 325 |
operations should be used explicitly in those circumstances. |
| 326 |
.PP |
| 327 |
You only need to make an instance of \fBoo::Slot\fR if you are definining your |
| 328 |
own slot that behaves like a standard slot. |
| 329 |
.PP |
| 330 |
.SS "SLOT IMPLEMENTATION" |
| 331 |
Internally, slot objects also define a method \fB\-\-default\-operation\fR |
| 332 |
which is forwarded to the default operation of the slot (thus, for the class |
| 333 |
.QW \fBvariable\fR |
| 334 |
slot, this is forwarded to |
| 335 |
.QW "\fBmy \-append\fR" ), |
| 336 |
and these methods which provide the implementation interface: |
| 337 |
.VE |
| 338 |
.TP |
| 339 |
\fIslot\fR \fBGet\fR |
| 340 |
.VS |
| 341 |
Returns a list that is the current contents of the slot. This method must |
| 342 |
always be called from a stack frame created by a call to \fBoo::define\fR or |
| 343 |
\fBoo::objdefine\fR. |
| 344 |
.VE |
| 345 |
.TP |
| 346 |
\fIslot\fR \fBResolve \fIelement\fR |
| 347 |
.VS |
| 348 |
This converts an element of the slotted collection into its resolved form; for |
| 349 |
a simple value, it could just return the value, but for a slot that contains |
| 350 |
references to commands or classes it should convert those into their |
| 351 |
fully-qualified forms (so they can be compared with \fBstring equals\fR): that |
| 352 |
could be done by forwarding to \fBnamespace which\fR or similar. |
| 353 |
.VE |
| 354 |
.TP |
| 355 |
\fIslot\fR \fBSet \fIelementList\fR |
| 356 |
.VS |
| 357 |
Sets the contents of the slot to the list \fIelementList\fR and returns the |
| 358 |
empty string. This method must always be called from a stack frame created by |
| 359 |
a call to \fBoo::define\fR or \fBoo::objdefine\fR. |
| 360 |
.PP |
| 361 |
The implementation of these methods is slot-dependent (and responsible for |
| 362 |
accessing the correct part of the class or object definition). Slots also have |
| 363 |
an unknown method handler to tie all these pieces together, and they hide |
| 364 |
their \fBdestroy\fR method so that it is not invoked inadvertently. It is |
| 365 |
\fIrecommended\fR that any user changes to the slot mechanism itself be |
| 366 |
restricted to defining new operations whose names start with a hyphen. |
| 367 |
.PP |
| 368 |
Note that slot instances are not expected to contain the storage for the slot |
| 369 |
they manage; that will be in or attached to the class or object that they |
| 370 |
manage. Those instances should provide their own implementations of the |
| 371 |
\fBGet\fR and \fBSet\fR methods (and optionally \fBResolve\fR; that defaults |
| 372 |
to a do-nothing pass-through). |
| 373 |
.VE |
| 374 |
.SH EXAMPLES |
| 375 |
This example demonstrates how to use both forms of the \fBoo::define\fR and |
| 376 |
\fBoo::objdefine\fR commands (they work in the same way), as well as |
| 377 |
illustrating four of the subcommands of them. |
| 378 |
.PP |
| 379 |
.CS |
| 380 |
oo::class create c |
| 381 |
c create o |
| 382 |
\fBoo::define\fR c \fBmethod\fR foo {} { |
| 383 |
puts "world" |
| 384 |
} |
| 385 |
\fBoo::objdefine\fR o { |
| 386 |
\fBmethod\fR bar {} { |
| 387 |
my Foo "hello " |
| 388 |
my foo |
| 389 |
} |
| 390 |
\fBforward\fR Foo ::puts -nonewline |
| 391 |
\fBunexport\fR foo |
| 392 |
} |
| 393 |
o bar \fI\(-> prints "hello world"\fR |
| 394 |
o foo \fI\(-> error "unknown method foo"\fR |
| 395 |
o Foo Bar \fI\(-> error "unknown method Foo"\fR |
| 396 |
\fBoo::objdefine\fR o \fBrenamemethod\fR bar lollipop |
| 397 |
o lollipop \fI\(-> prints "hello world"\fR |
| 398 |
.CE |
| 399 |
.PP |
| 400 |
This example shows how additional classes can be mixed into an object. It also |
| 401 |
shows how \fBmixin\fR is a slot that supports appending: |
| 402 |
.PP |
| 403 |
.CS |
| 404 |
oo::object create inst |
| 405 |
inst m1 \fI\(-> error "unknown method m1"\fR |
| 406 |
inst m2 \fI\(-> error "unknown method m2"\fR |
| 407 |
|
| 408 |
oo::class create A { |
| 409 |
\fBmethod\fR m1 {} { |
| 410 |
puts "red brick" |
| 411 |
} |
| 412 |
} |
| 413 |
\fBoo::objdefine\fR inst { |
| 414 |
\fBmixin\fR A |
| 415 |
} |
| 416 |
inst m1 \fI\(-> prints "red brick"\fR |
| 417 |
inst m2 \fI\(-> error "unknown method m2"\fR |
| 418 |
|
| 419 |
oo::class create B { |
| 420 |
\fBmethod\fR m2 {} { |
| 421 |
puts "blue brick" |
| 422 |
} |
| 423 |
} |
| 424 |
\fBoo::objdefine\fR inst { |
| 425 |
\fBmixin -append\fR B |
| 426 |
} |
| 427 |
inst m1 \fI\(-> prints "red brick"\fR |
| 428 |
inst m2 \fI\(-> prints "blue brick"\fR |
| 429 |
.CE |
| 430 |
.SH "SEE ALSO" |
| 431 |
next(n), oo::class(n), oo::object(n) |
| 432 |
.SH KEYWORDS |
| 433 |
class, definition, method, object, slot |
| 434 |
.\" Local variables: |
| 435 |
.\" mode: nroff |
| 436 |
.\" fill-column: 78 |
| 437 |
.\" End: |