<h1>next and nextto</h1>
<DL style="font-size:.85em">
<DD><A HREF="#M2" NAME="L1262">NAME</A>
<DL><DD>next, nextto &mdash; invoke superclass method
<DD><A HREF="#M3" NAME="L1263">SYNOPSIS</A>
<DL>
</DL>
<DD><A HREF="#M4" NAME="L1264">DESCRIPTION</A>
<DD><A HREF="#M5" NAME="L1265">THE METHOD CHAIN</A>
<OL class="the method chain">
</OL>
<DD><A HREF="#M6" NAME="L1266">METHOD SEARCH ORDER</A>
<OL class="method search order">
</OL>
<DD><A HREF="#M7" NAME="L1267">FILTERS</A>
<DD><A HREF="#M8" NAME="L1268">EXAMPLES</A>
<DD><A HREF="#M9" NAME="L1269">SEE ALSO</A>
<DD><A HREF="#M10" NAME="L1270">KEYWORDS</A>
</DL>
<H3><A
<b>next</b>, <b>nextto</b> &mdash; invoke superclass method implementations
<H3><A
package require TclOO<BR>
<BR>
<B>next</B> ?<I>arg ...</I>?<BR>
<B>nextto</B><I> class</I> ?<I>arg ...</I>?<BR>
<H3><A
The <B>next</B> command is used to call implementations of a method by a class,
superclass or mixin that are overridden by the current method. It can only be
used from within a method. It is also used within filters to indicate the
point where a filter calls the actual implementation (the filter may decide to
not go along the chain, and may process the results of going along the chain
of methods as it chooses). The result of the <B>next</B> command is the result
of the next method in the method chain; if there are no further methods in the
method chain, the result of <B>next</B> will be an error. The arguments,
<I>arg</I>, to <B>next</B> are the arguments to pass to the next method in the
chain.
<P>
The <B>nextto</B> command is the same as the <B>next</B> command, except that it
takes an additional <I>class</I> argument that identifies a class whose
implementation of the current method chain (see <B><A <B>call</B>) should
be used; the method implementation selected will be the one provided by the
given class, and it must refer to an existing non-filter invocation that lies
further along the chain than the current implementation.
<H3><A NAME="M5">THE METHOD
When a method of an object is invoked, things happen in several stages:
<P>
<OL class="the method chain">
<LI value="1">
The structure of the object, its class, superclasses, filters, and mixins, are
examined to build a <I>method chain</I>, which contains a list of method
implementations to invoke.
<P><LI value="2">
The first method implementation on the chain is invoked.
<P><LI value="3">
If that method implementation invokes the <B>next</B> command, the next method
implementation is invoked (with its arguments being those that were passed to
<B>next</B>).
<P><LI value="4">
The result from the overall method call is the result from the outermost
method implementation; inner method implementations return their results
through <B>next</B>.
<P><LI value="5">
The method chain is cached for future use.
<P></OL>
<H4><A NAME="M6">METHOD SEARCH
When constructing the method chain, method implementations are searched for in
the following order:
<P>
<OL class="method search order">
<LI value="1">
In the classes mixed into the object, in class traversal order. The list of
mixins is checked in natural order.
<P><LI value="2">
In the classes mixed into the classes of the object, with sources of mixing in
being searched in class traversal order. Within each class, the list of mixins
is processed in natural order.
<P><LI value="3">
In the object itself.
<P><LI value="4">
In the object's class.
<P><LI value="5">
In the superclasses of the class, following each superclass in a depth-first
fashion in the natural order of the superclass list.
<P></OL>
<P>
Any particular method implementation always comes as <I>late</I> in the
resulting list of implementations as possible; this means that if some class,
A, is both mixed into a class, B, and is also a superclass of B, the instances
of B will always treat A as a superclass from the perspective of inheritance.
This is true even when the multiple inheritance is processed indirectly.
<H4><A
When an object has a list of filter names set upon it, or is an instance of a
class (or has mixed in a class) that has a list of filter names set upon it,
before every invocation of any method the filters are processed. Filter
implementations are found in class traversal order, as are the lists of filter
names (each of which is traversed in natural list order). Explicitly invoking
a method used as a filter will cause that method to be invoked twice, once as
a filter and once as a normal method.
<P>
Each filter should decide for itself whether to permit the execution to go
forward to the proper implementation of the method (which it does by invoking
the <B>next</B> command as filters are inserted into the front of the method
call chain) and is responsible for returning the result of <B>next</B>.
<P>
Filters are invoked when processing an invokation of the <B>unknown</B>
method because of a failure to locate a method implementation, but <I>not</I>
when invoking either constructors or destructors. (Note however that the
<B>destroy</B> method is a conventional method, and filters are invoked as
normal when it is called.)
<H3><A
This example demonstrates how to use the <B>next</B> command to call the
(super)class's implementation of a method. The script:
<P>
<PRE>oo::class create theSuperclass {
    method example {args} {
        puts &quot;in the superclass, args = $args&quot;
    }
}
oo::class create theSubclass {
    superclass theSuperclass
    method example {args} {
        puts &quot;before chaining from subclass, args = $args&quot;
        <B>next</B> a {\*}$args b
        <B>next</B> pureSynthesis
        puts &quot;after chaining from subclass&quot;
    }
}
theSubclass create obj
oo::objdefine obj method example args {
    puts &quot;per-object method, args = $args&quot;
    <B>next</B> x {\*}$args y
    <B>next</B>
}
obj example 1 2 3</PRE>
<P>
prints the following:
<P>
<PRE>per-object method, args = 1 2 3
before chaining from subclass, args = x 1 2 3 y
in the superclass, args = a x 1 2 3 y b
in the superclass, args = pureSynthesis
after chaining from subclass
before chaining from subclass, args =
in the superclass, args = a b
in the superclass, args = pureSynthesis
after chaining from subclass</PRE>
<P>
This example demonstrates how to build a simple cache class that applies
memoization to all the method calls of the objects it is mixed into, and shows
how it can make a difference to computation times:
<P>
<PRE>oo::class create cache {
    filter Memoize
    method Memoize args {
        <I># Do not filter the core method implementations</I>
        if {[lindex [self target] 0] eq &quot;::oo::object&quot;} {
            return [<B>next</B> {\*}$args]
        }<br>
        <I># Check if the value is already in the cache</I>
        my variable ValueCache
        set key [self target],$args
        if {[info exist ValueCache($key)]} {
            return $ValueCache($key)
        }<br>
        <I># Compute value, insert into cache, and return it</I>
        return [set ValueCache($key) [<B>next</B> {\*}$args]]
    }
    method flushCache {} {
        my variable ValueCache
        unset ValueCache
        <I># Skip the caching</I>
        return -level 2 &quot;&quot;
    }
}<br>
oo::object create demo
oo::objdefine demo {
    mixin cache
    method compute {a b c} {
        after 3000 <I>;# Simulate deep thought</I>
        return [expr {$a + $b * $c}]
    }
    method compute2 {a b c} {
        after 3000 <I>;# Simulate deep thought</I>
        return [expr {$a * $b + $c}]
    }
}<br>
puts [demo compute  1 2 3]      <I><font size="+1">&#8594;</font> prints &quot;7&quot; after delay</I>
puts [demo compute2 4 5 6]      <I><font size="+1">&#8594;</font> prints &quot;26&quot; after delay</I>
puts [demo compute  1 2 3]      <I><font size="+1">&#8594;</font> prints &quot;7&quot; instantly</I>
puts [demo compute2 4 5 6]      <I><font size="+1">&#8594;</font> prints &quot;26&quot; instantly</I>
puts [demo compute  4 5 6]      <I><font size="+1">&#8594;</font> prints &quot;34&quot; after delay</I>
puts [demo compute  4 5 6]      <I><font size="+1">&#8594;</font> prints &quot;34&quot; instantly</I>
puts [demo compute  1 2 3]      <I><font size="+1">&#8594;</font> prints &quot;7&quot; instantly</I>
demo flushCache
puts [demo compute  1 2 3]      <I><font size="+1">&#8594;</font> prints &quot;7&quot; after
<H3><A NAME="M9">SEE
<B><A <B><A <B><A <B><A
<H3><A
call, method, method chain