| 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 self 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 |
self \- method call internal introspection |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
package require TclOO |
| 16 |
|
| 17 |
\fBself\fR ?\fIsubcommand\fR? |
| 18 |
.fi |
| 19 |
.BE |
| 20 |
.SH DESCRIPTION |
| 21 |
The \fBself\fR command, which should only be used from within the context of a |
| 22 |
call to a method (i.e. inside a method, constructor or destructor body) is |
| 23 |
used to allow the method to discover information about how it was called. It |
| 24 |
takes an argument, \fIsubcommand\fR, that tells it what sort of information is |
| 25 |
actually desired; if omitted the result will be the same as if \fBself |
| 26 |
object\fR was invoked. The supported subcommands are: |
| 27 |
.TP |
| 28 |
\fBself call\fR |
| 29 |
. |
| 30 |
This returns a two-element list describing the method implementations used to |
| 31 |
implement the current call chain. The first element is the same as would be |
| 32 |
reported by \fBinfo object\fR \fBcall\fR for the current method (except that this |
| 33 |
also reports useful values from within constructors and destructors, whose |
| 34 |
names are reported as \fB<constructor>\fR and \fB<destructor>\fR |
| 35 |
respectively), and the second element is an index into the first element's |
| 36 |
list that indicates which actual implementation is currently executing (the |
| 37 |
first implementation to execute is always at index 0). |
| 38 |
.TP |
| 39 |
\fBself caller\fR |
| 40 |
. |
| 41 |
When the method was invoked from inside another object method, this subcommand |
| 42 |
returns a three element list describing the containing object and method. The |
| 43 |
first element describes the declaring object or class of the method, the |
| 44 |
second element is the name of the object on which the containing method was |
| 45 |
invoked, and the third element is the name of the method (with the strings |
| 46 |
\fB<constructor>\fR and \fB<destructor>\fR indicating constructors and |
| 47 |
destructors respectively). |
| 48 |
.TP |
| 49 |
\fBself class\fR |
| 50 |
. |
| 51 |
This returns the name of the class that the current method was defined within. |
| 52 |
Note that this will change as the chain of method implementations is traversed |
| 53 |
with \fBnext\fR, and that if the method was defined on an object then this |
| 54 |
will fail. |
| 55 |
.RS |
| 56 |
.PP |
| 57 |
If you want the class of the current object, you need to use this other |
| 58 |
construct: |
| 59 |
.PP |
| 60 |
.CS |
| 61 |
info object class [\fBself object\fR] |
| 62 |
.CE |
| 63 |
.RE |
| 64 |
.TP |
| 65 |
\fBself filter\fR |
| 66 |
. |
| 67 |
When invoked inside a filter, this subcommand returns a three element list |
| 68 |
describing the filter. The first element gives the name of the object or class |
| 69 |
that declared the filter (note that this may be different from the object or |
| 70 |
class that provided the implementation of the filter), the second element is |
| 71 |
either \fBobject\fR or \fBclass\fR depending on whether the declaring entity |
| 72 |
was an object or class, and the third element is the name of the filter. |
| 73 |
.TP |
| 74 |
\fBself method\fR |
| 75 |
. |
| 76 |
This returns the name of the current method (with the strings |
| 77 |
\fB<constructor>\fR and \fB<destructor>\fR indicating constructors and |
| 78 |
destructors respectively). |
| 79 |
.TP |
| 80 |
\fBself namespace\fR |
| 81 |
. |
| 82 |
This returns the name of the unique namespace of the object that the method |
| 83 |
was invoked upon. |
| 84 |
.TP |
| 85 |
\fBself next\fR |
| 86 |
. |
| 87 |
When invoked from a method that is not at the end of a call chain (i.e. where |
| 88 |
the \fBnext\fR command will invoke an actual method implementation), this |
| 89 |
subcommand returns a two element list describing the next element in the |
| 90 |
method call chain; the first element is the name of the class or object that |
| 91 |
declares the next part of the call chain, and the second element is the name |
| 92 |
of the method (with the strings \fB<constructor>\fR and \fB<destructor>\fR |
| 93 |
indicating constructors and destructors respectively). If invoked from a |
| 94 |
method that is at the end of a call chain, this subcommand returns the empty |
| 95 |
string. |
| 96 |
.TP |
| 97 |
\fBself object\fR |
| 98 |
. |
| 99 |
This returns the name of the object that the method was invoked upon. |
| 100 |
.TP |
| 101 |
\fBself target\fR |
| 102 |
. |
| 103 |
When invoked inside a filter implementation, this subcommand returns a two |
| 104 |
element list describing the method being filtered. The first element will be |
| 105 |
the name of the declarer of the method, and the second element will be the |
| 106 |
actual name of the method. |
| 107 |
.SH EXAMPLES |
| 108 |
.PP |
| 109 |
This example shows basic use of \fBself\fR to provide information about the |
| 110 |
current object: |
| 111 |
.PP |
| 112 |
.CS |
| 113 |
oo::class create c { |
| 114 |
method foo {} { |
| 115 |
puts "this is the [\fBself\fR] object" |
| 116 |
} |
| 117 |
} |
| 118 |
c create a |
| 119 |
c create b |
| 120 |
a foo \fI\(-> prints "this is the ::a object"\fR |
| 121 |
b foo \fI\(-> prints "this is the ::b object"\fR |
| 122 |
.CE |
| 123 |
.PP |
| 124 |
This demonstrates what a method call chain looks like, and how traversing |
| 125 |
along it changes the index into it: |
| 126 |
.PP |
| 127 |
.CS |
| 128 |
oo::class create c { |
| 129 |
method x {} { |
| 130 |
puts "Cls: [\fBself call\fR]" |
| 131 |
} |
| 132 |
} |
| 133 |
c create a |
| 134 |
oo::objdefine a { |
| 135 |
method x {} { |
| 136 |
puts "Obj: [\fBself call\fR]" |
| 137 |
next |
| 138 |
puts "Obj: [\fBself call\fR]" |
| 139 |
} |
| 140 |
} |
| 141 |
a x \fI\(-> Obj: {{method x object method} {method x ::c method}} 0\fR |
| 142 |
\fI\(-> Cls: {{method x object method} {method x ::c method}} 1\fR |
| 143 |
\fI\(-> Obj: {{method x object method} {method x ::c method}} 0\fR |
| 144 |
.CE |
| 145 |
.SH "SEE ALSO" |
| 146 |
info(n), next(n) |
| 147 |
.SH KEYWORDS |
| 148 |
call, introspection, object |
| 149 |
.\" Local variables: |
| 150 |
.\" mode: nroff |
| 151 |
.\" fill-column: 78 |
| 152 |
.\" End: |