| 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 class 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::class \- class of all classes |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
package require TclOO |
| 16 |
|
| 17 |
\fBoo::class\fI method \fR?\fIarg ...\fR? |
| 18 |
.fi |
| 19 |
.SH "CLASS HIERARCHY" |
| 20 |
.nf |
| 21 |
\fBoo::object\fR |
| 22 |
\(-> \fBoo::class\fR |
| 23 |
.fi |
| 24 |
.BE |
| 25 |
.SH DESCRIPTION |
| 26 |
.PP |
| 27 |
Classes are objects that can manufacture other objects according to a pattern |
| 28 |
stored in the factory object (the class). An instance of the class is created |
| 29 |
by calling one of the class's factory methods, typically either \fBcreate\fR |
| 30 |
if an explicit name is being given, or \fBnew\fR if an arbitrary unique name |
| 31 |
is to be automatically selected. |
| 32 |
.PP |
| 33 |
The \fBoo::class\fR class is the class of all classes; every class is an |
| 34 |
instance of this class, which is consequently an instance of itself. This |
| 35 |
class is a subclass of \fBoo::object\fR, so every class is also an object. |
| 36 |
Additional metaclasses (i.e., classes of classes) can be defined if necessary |
| 37 |
by subclassing \fBoo::class\fR. Note that the \fBoo::class\fR object hides the |
| 38 |
\fBnew\fR method on itself, so new classes should always be made using the |
| 39 |
\fBcreate\fR method. |
| 40 |
.SS CONSTRUCTOR |
| 41 |
.PP |
| 42 |
The constructor of the \fBoo::class\fR class takes an optional argument which, |
| 43 |
if present, is sent to the \fBoo::define\fR command (along with the name of |
| 44 |
the newly-created class) to allow the class to be conveniently configured at |
| 45 |
creation time. |
| 46 |
.SS DESTRUCTOR |
| 47 |
The \fBoo::class\fR class does not define an explicit destructor. However, |
| 48 |
when a class is destroyed, all its subclasses and instances are also |
| 49 |
destroyed, along with all objects that it has been mixed into. |
| 50 |
.SS "EXPORTED METHODS" |
| 51 |
.TP |
| 52 |
\fIcls \fBcreate \fIname \fR?\fIarg ...\fR? |
| 53 |
. |
| 54 |
This creates a new instance of the class \fIcls\fR called \fIname\fR (which is |
| 55 |
resolved within the calling context's namespace if not fully qualified), |
| 56 |
passing the arguments, \fIarg ...\fR, to the constructor, and (if that returns |
| 57 |
a successful result) returning the fully qualified name of the created object |
| 58 |
(the result of the constructor is ignored). If the constructor fails (i.e. |
| 59 |
returns a non-OK result) then the object is destroyed and the error message is |
| 60 |
the result of this method call. |
| 61 |
.TP |
| 62 |
\fIcls \fBnew \fR?\fIarg ...\fR? |
| 63 |
. |
| 64 |
This creates a new instance of the class \fIcls\fR with a new unique name, |
| 65 |
passing the arguments, \fIarg ...\fR, to the constructor, and (if that returns |
| 66 |
a successful result) returning the fully qualified name of the created object |
| 67 |
(the result of the constructor is ignored). If the constructor fails (i.e., |
| 68 |
returns a non-OK result) then the object is destroyed and the error message is |
| 69 |
the result of this method call. |
| 70 |
.RS |
| 71 |
.PP |
| 72 |
Note that this method is not exported by the \fBoo::class\fR object itself, so |
| 73 |
classes should not be created using this method. |
| 74 |
.RE |
| 75 |
.SS "NON-EXPORTED METHODS" |
| 76 |
.PP |
| 77 |
The \fBoo::class\fR class supports the following non-exported methods: |
| 78 |
.TP |
| 79 |
\fIcls \fBcreateWithNamespace\fI name nsName\fR ?\fIarg ...\fR? |
| 80 |
. |
| 81 |
This creates a new instance of the class \fIcls\fR called \fIname\fR (which is |
| 82 |
resolved within the calling context's namespace if not fully qualified), |
| 83 |
passing the arguments, \fIarg ...\fR, to the constructor, and (if that returns |
| 84 |
a successful result) returning the fully qualified name of the created object |
| 85 |
(the result of the constructor is ignored). The name of the instance's |
| 86 |
internal namespace will be \fInsName\fR; |
| 87 |
.VS |
| 88 |
it is an error if that namespace cannot be created. |
| 89 |
.VE |
| 90 |
If the constructor fails |
| 91 |
(i.e., returns a non-OK result) then the object is destroyed and the error |
| 92 |
message is the result of this method call. |
| 93 |
.SH EXAMPLES |
| 94 |
.PP |
| 95 |
This example defines a simple class hierarchy and creates a new instance of |
| 96 |
it. It then invokes a method of the object before destroying the hierarchy and |
| 97 |
showing that the destruction is transitive. |
| 98 |
.PP |
| 99 |
.CS |
| 100 |
\fBoo::class create\fR fruit { |
| 101 |
method eat {} { |
| 102 |
puts "yummy!" |
| 103 |
} |
| 104 |
} |
| 105 |
\fBoo::class create\fR banana { |
| 106 |
superclass fruit |
| 107 |
constructor {} { |
| 108 |
my variable peeled |
| 109 |
set peeled 0 |
| 110 |
} |
| 111 |
method peel {} { |
| 112 |
my variable peeled |
| 113 |
set peeled 1 |
| 114 |
puts "skin now off" |
| 115 |
} |
| 116 |
method edible? {} { |
| 117 |
my variable peeled |
| 118 |
return $peeled |
| 119 |
} |
| 120 |
method eat {} { |
| 121 |
if {![my edible?]} { |
| 122 |
my peel |
| 123 |
} |
| 124 |
next |
| 125 |
} |
| 126 |
} |
| 127 |
set b [banana \fBnew\fR] |
| 128 |
$b eat \fI\(-> prints "skin now off" and "yummy!"\fR |
| 129 |
fruit destroy |
| 130 |
$b eat \fI\(-> error "unknown command"\fR |
| 131 |
.CE |
| 132 |
.SH "SEE ALSO" |
| 133 |
oo::define(n), oo::object(n) |
| 134 |
.SH KEYWORDS |
| 135 |
class, metaclass, object |
| 136 |
.\" Local variables: |
| 137 |
.\" mode: nroff |
| 138 |
.\" fill-column: 78 |
| 139 |
.\" End: |