TclOO Package

Artifact [60860b6ab7]
Login

Artifact 60860b6ab750ab7419cd568fb477220c1978a272:

Wiki page [Doc: self] by dkf 2016-09-17 11:37:28.
D 2016-09-17T11:37:28.685
L Doc:\sself
N text/x-markdown
U dkf
W 6770
<h1>self</h1>
<DL style="font-size:.85em">
<DD><A HREF="#M2" NAME="L1713">NAME</A>
<DL><DD>self &mdash; method call internal introspection</DD></DL>
<DD><A HREF="#M3" NAME="L1714">SYNOPSIS</A>
<DL>
</DL>
<DD><A HREF="#M4" NAME="L1715">DESCRIPTION</A>
<DL class="description">
<DD><A HREF="#M5" NAME="L1716"><B>self call</B></A>
<DD><A HREF="#M6" NAME="L1717"><B>self caller</B></A>
<DD><A HREF="#M7" NAME="L1718"><B>self class</B></A>
<DD><A HREF="#M8" NAME="L1719"><B>self filter</B></A>
<DD><A HREF="#M9" NAME="L1720"><B>self method</B></A>
<DD><A HREF="#M10" NAME="L1721"><B>self namespace</B></A>
<DD><A HREF="#M11" NAME="L1722"><B>self next</B></A>
<DD><A HREF="#M12" NAME="L1723"><B>self object</B></A>
<DD><A HREF="#M13" NAME="L1724"><B>self target</B></A>
</DL>
<DD><A HREF="#M14" NAME="L1725">EXAMPLES</A>
<DD><A HREF="#M15" NAME="L1726">SEE ALSO</A>
<DD><A HREF="#M16" NAME="L1727">KEYWORDS</A>
</DL>
<H3><A NAME="M2">NAME</A></H3>
<b>self</b> &mdash; method call internal introspection
<H3><A NAME="M3">SYNOPSIS</A></H3>
package require TclOO<BR>
<BR>
<B>self</B> ?<I>subcommand</I>?<BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
The <B>self</B> command, which should only be used from within the context of a
call to a method (i.e. inside a method, constructor or destructor body) is
used to allow the method to discover information about how it was called. It
takes an argument, <I>subcommand</I>, that tells it what sort of information is
actually desired; if omitted the result will be the same as if <B>self
object</B> was invoked. The supported subcommands are:
<P>
<DL class="description">
<DT><A NAME="M5"><B>self call</B></A><DD>
This returns a two-element list describing the method implementations used to
implement the current call chain. The first element is the same as would be
reported by <B><A HREF="?name=Doc:+info">info object</A></B> <B>call</B> for the current method (except that this
also reports useful values from within constructors and destructors, whose
names are reported as <B>&lt;constructor&gt;</B> and <B>&lt;destructor&gt;</B>
respectively), and the second element is an index into the first element's
list that indicates which actual implementation is currently executing (the
first implementation to execute is always at index 0).
<P><DT><A NAME="M6"><B>self caller</B></A><DD>
When the method was invoked from inside another object method, this subcommand
returns a three element list describing the containing object and method. The
first element describes the declaring object or class of the method, the
second element is the name of the object on which the containing method was
invoked, and the third element is the name of the method (with the strings
<B>&lt;constructor&gt;</B> and <B>&lt;destructor&gt;</B> indicating constructors and
destructors respectively).
<P><DT><A NAME="M7"><B>self class</B></A><DD>
This returns the name of the class that the current method was defined within.
Note that this will change as the chain of method implementations is traversed
with <B><A HREF="?name=Doc:+next">next</A></B>, and that if the method was defined on an object then this
will fail.
<P>
If you want the class of the current object, you need to use this other
construct:
<P>
<PRE>info object class [<B>self object</B>]</PRE>
<P><DT><A NAME="M8"><B>self filter</B></A><DD>
When invoked inside a filter, this subcommand returns a three element list
describing the filter. The first element gives the name of the object or class
that declared the filter (note that this may be different from the object or
class that provided the implementation of the filter), the second element is
either <B>object</B> or <B>class</B> depending on whether the declaring entity
was an object or class, and the third element is the name of the filter.
<P><DT><A NAME="M9"><B>self method</B></A><DD>
This returns the name of the current method (with the strings
<B>&lt;constructor&gt;</B> and <B>&lt;destructor&gt;</B> indicating constructors and
destructors respectively).
<P><DT><A NAME="M10"><B>self namespace</B></A><DD>
This returns the name of the unique namespace of the object that the method
was invoked upon.
<P><DT><A NAME="M11"><B>self next</B></A><DD>
When invoked from a method that is not at the end of a call chain (i.e. where
the <B><A HREF="?name=Doc:+next">next</A></B> command will invoke an actual method implementation), this
subcommand returns a two element list describing the next element in the
method call chain; the first element is the name of the class or object that
declares the next part of the call chain, and the second element is the name
of the method (with the strings <B>&lt;constructor&gt;</B> and <B>&lt;destructor&gt;</B>
indicating constructors and destructors respectively). If invoked from a
method that is at the end of a call chain, this subcommand returns the empty
string.
<P><DT><A NAME="M12"><B>self object</B></A><DD>
This returns the name of the object that the method was invoked upon.
<P><DT><A NAME="M13"><B>self target</B></A><DD>
When invoked inside a filter implementation, this subcommand returns a two
element list describing the method being filtered. The first element will be
the name of the declarer of the method, and the second element will be the
actual name of the method.
<P></DL>
<H3><A NAME="M14">EXAMPLES</A></H3>
This example shows basic use of <B>self</B> to provide information about the
current object:
<P>
<PRE>oo::class create c {
    method foo {} {
        puts &quot;this is the [<B>self</B>] object&quot;
    }
}
c create a
c create b
a foo                <I><font size="+1">&#8594;</font> prints &quot;this is the ::a object&quot;</I>
b foo                <I><font size="+1">&#8594;</font> prints &quot;this is the ::b object&quot;</I></PRE>
<P>
This demonstrates what a method call chain looks like, and how traversing
along it changes the index into it:
<P>
<PRE>oo::class create c {
    method x {} {
        puts &quot;Cls: [<B>self call</B>]&quot;
    }
}
c create a
oo::objdefine a {
    method x {} {
        puts &quot;Obj: [<B>self call</B>]&quot;
        next
        puts &quot;Obj: [<B>self call</B>]&quot;
    }
}
a x     <I><font size="+1">&#8594;</font> Obj: {{method x object method} {method x ::c method}} 0</I>
        <I><font size="+1">&#8594;</font> Cls: {{method x object method} {method x ::c method}} 1</I>
        <I><font size="+1">&#8594;</font> Obj: {{method x object method} {method x ::c method}} 0</I></PRE>
<H3><A NAME="M15">SEE ALSO</A></H3>
<B><A HREF="?name=Doc:+info">info</A></B>, <B><A HREF="?name=Doc:+next">next</A></B>
<H3><A NAME="M16">KEYWORDS</A></H3>
call, introspection, object
Z 15cfc0b5a4f5d6836b167340d16eba50