| 1 |
'\" |
| 2 |
'\" Copyright (c) 2007-2008 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 object n 0.1 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::object \- root class of the class hierarchy |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
package require TclOO |
| 16 |
|
| 17 |
\fBoo::object\fI method \fR?\fIarg ...\fR? |
| 18 |
.fi |
| 19 |
.SH "CLASS HIERARCHY" |
| 20 |
.nf |
| 21 |
\fBoo::object\fR |
| 22 |
.fi |
| 23 |
.BE |
| 24 |
.SH DESCRIPTION |
| 25 |
.PP |
| 26 |
The \fBoo::object\fR class is the root class of the object hierarchy; every |
| 27 |
object is an instance of this class. Since classes are themselves objects, |
| 28 |
they are instances of this class too. Objects are always referred to by their |
| 29 |
name, and may be \fBrename\fRd while maintaining their identity. |
| 30 |
.PP |
| 31 |
Instances of objects may be made with either the \fBcreate\fR or \fBnew\fR |
| 32 |
methods of the \fBoo::object\fR object itself, or by invoking those methods on |
| 33 |
any of the subclass objects; see \fBoo::class\fR for more details. The |
| 34 |
configuration of individual objects (i.e., instance-specific methods, mixed-in |
| 35 |
classes, etc.) may be controlled with the \fBoo::objdefine\fR command. |
| 36 |
.PP |
| 37 |
Each object has a unique namespace associated with it, the instance namespace. |
| 38 |
This namespace holds all the instance variables of the object, and will be the |
| 39 |
current namespace whenever a method of the object is invoked (including a |
| 40 |
method of the class of the object). When the object is destroyed, its instance |
| 41 |
namespace is deleted. The instance namespace contains the object's \fBmy\fR |
| 42 |
command, which may be used to invoke non-exported methods of the object or to |
| 43 |
create a reference to the object for the purpose of invocation which persists |
| 44 |
across renamings of the object. |
| 45 |
.SS CONSTRUCTOR |
| 46 |
The \fBoo::object\fR class does not define an explicit constructor. |
| 47 |
.SS DESTRUCTOR |
| 48 |
The \fBoo::object\fR class does not define an explicit destructor. |
| 49 |
.SS "EXPORTED METHODS" |
| 50 |
The \fBoo::object\fR class supports the following exported methods: |
| 51 |
.TP |
| 52 |
\fIobj \fBdestroy\fR |
| 53 |
. |
| 54 |
This method destroys the object, \fIobj\fR, that it is invoked upon, invoking |
| 55 |
any destructors on the object's class in the process. It is equivalent to |
| 56 |
using \fBrename\fR to delete the object command. The result of this method is |
| 57 |
always the empty string. |
| 58 |
.SS "NON-EXPORTED METHODS" |
| 59 |
.PP |
| 60 |
The \fBoo::object\fR class supports the following non-exported methods: |
| 61 |
.TP |
| 62 |
\fIobj \fBeval\fR ?\fIarg ...\fR? |
| 63 |
. |
| 64 |
This method concatenates the arguments, \fIarg\fR, as if with \fBconcat\fR, |
| 65 |
and then evaluates the resulting script in the namespace that is uniquely |
| 66 |
associated with \fIobj\fR, returning the result of the evaluation. |
| 67 |
.RS |
| 68 |
.PP |
| 69 |
Note that object-internal commands such as \fBmy\fR and \fBself\fR can be |
| 70 |
invoked in this context. |
| 71 |
.RE |
| 72 |
.TP |
| 73 |
\fIobj \fBunknown ?\fImethodName\fR? ?\fIarg ...\fR? |
| 74 |
. |
| 75 |
This method is called when an attempt to invoke the method \fImethodName\fR on |
| 76 |
object \fIobj\fR fails. The arguments that the user supplied to the method are |
| 77 |
given as \fIarg\fR arguments. |
| 78 |
.VS |
| 79 |
If \fImethodName\fR is absent, the object was invoked with no method name at |
| 80 |
all (or any other arguments). |
| 81 |
.VE |
| 82 |
The default implementation (i.e., the one defined by the \fBoo::object\fR |
| 83 |
class) generates a suitable error, detailing what methods the object supports |
| 84 |
given whether the object was invoked by its public name or through the |
| 85 |
\fBmy\fR command. |
| 86 |
.TP |
| 87 |
\fIobj \fBvariable \fR?\fIvarName ...\fR? |
| 88 |
. |
| 89 |
This method arranges for each variable called \fIvarName\fR to be linked from |
| 90 |
the object \fIobj\fR's unique namespace into the caller's context. Thus, if it |
| 91 |
is invoked from inside a procedure then the namespace variable in the object |
| 92 |
is linked to the local variable in the procedure. Each \fIvarName\fR argument |
| 93 |
must not have any namespace separators in it. The result is the empty string. |
| 94 |
.TP |
| 95 |
\fIobj \fBvarname \fIvarName\fR |
| 96 |
. |
| 97 |
This method returns the globally qualified name of the variable \fIvarName\fR |
| 98 |
in the unique namespace for the object \fIobj\fR. |
| 99 |
.TP |
| 100 |
\fIobj \fB<cloned> \fIsourceObjectName\fR |
| 101 |
.VS |
| 102 |
This method is used by the \fBoo::object\fR command to copy the state of one |
| 103 |
object to another. It is responsible for copying the procedures and variables |
| 104 |
of the namespace of the source object (\fIsourceObjectName\fR) to the current |
| 105 |
object. It does not copy any other types of commands or any traces on the |
| 106 |
variables; that can be added if desired by overriding this method in a |
| 107 |
subclass. |
| 108 |
.VE |
| 109 |
.SH EXAMPLES |
| 110 |
.PP |
| 111 |
This example demonstrates basic use of an object. |
| 112 |
.PP |
| 113 |
.CS |
| 114 |
set obj [\fBoo::object\fR new] |
| 115 |
$obj foo \fI\(-> error "unknown method foo"\fR |
| 116 |
oo::objdefine $obj method foo {} { |
| 117 |
my \fBvariable\fR count |
| 118 |
puts "bar[incr count]" |
| 119 |
} |
| 120 |
$obj foo \fI\(-> prints "bar1"\fR |
| 121 |
$obj foo \fI\(-> prints "bar2"\fR |
| 122 |
$obj variable count \fI\(-> error "unknown method variable"\fR |
| 123 |
$obj \fBdestroy\fR |
| 124 |
$obj foo \fI\(-> error "unknown command obj"\fR |
| 125 |
.CE |
| 126 |
.SH "SEE ALSO" |
| 127 |
my(n), oo::class(n) |
| 128 |
.SH KEYWORDS |
| 129 |
base class, class, object, root class |
| 130 |
.\" Local variables: |
| 131 |
.\" mode: nroff |
| 132 |
.\" fill-column: 78 |
| 133 |
.\" End: |