| 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 next 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 |
next, nextto \- invoke superclass method implementations |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
package require TclOO |
| 16 |
|
| 17 |
\fBnext\fR ?\fIarg ...\fR? |
| 18 |
\fBnextto\fI class\fR ?\fIarg ...\fR? |
| 19 |
.fi |
| 20 |
.BE |
| 21 |
|
| 22 |
.SH DESCRIPTION |
| 23 |
.PP |
| 24 |
The \fBnext\fR command is used to call implementations of a method by a class, |
| 25 |
superclass or mixin that are overridden by the current method. It can only be |
| 26 |
used from within a method. It is also used within filters to indicate the |
| 27 |
point where a filter calls the actual implementation (the filter may decide to |
| 28 |
not go along the chain, and may process the results of going along the chain |
| 29 |
of methods as it chooses). The result of the \fBnext\fR command is the result |
| 30 |
of the next method in the method chain; if there are no further methods in the |
| 31 |
method chain, the result of \fBnext\fR will be an error. The arguments, |
| 32 |
\fIarg\fR, to \fBnext\fR are the arguments to pass to the next method in the |
| 33 |
chain. |
| 34 |
.PP |
| 35 |
The \fBnextto\fR command is the same as the \fBnext\fR command, except that it |
| 36 |
takes an additional \fIclass\fR argument that identifies a class whose |
| 37 |
implementation of the current method chain (see \fBinfo object\fR \fBcall\fR) should |
| 38 |
be used; the method implementation selected will be the one provided by the |
| 39 |
given class, and it must refer to an existing non-filter invocation that lies |
| 40 |
further along the chain than the current implementation. |
| 41 |
.SH "THE METHOD CHAIN" |
| 42 |
.PP |
| 43 |
When a method of an object is invoked, things happen in several stages: |
| 44 |
.IP [1] |
| 45 |
The structure of the object, its class, superclasses, filters, and mixins, are |
| 46 |
examined to build a \fImethod chain\fR, which contains a list of method |
| 47 |
implementations to invoke. |
| 48 |
.IP [2] |
| 49 |
The first method implementation on the chain is invoked. |
| 50 |
.IP [3] |
| 51 |
If that method implementation invokes the \fBnext\fR command, the next method |
| 52 |
implementation is invoked (with its arguments being those that were passed to |
| 53 |
\fBnext\fR). |
| 54 |
.IP [4] |
| 55 |
The result from the overall method call is the result from the outermost |
| 56 |
method implementation; inner method implementations return their results |
| 57 |
through \fBnext\fR. |
| 58 |
.IP [5] |
| 59 |
The method chain is cached for future use. |
| 60 |
.SS "METHOD SEARCH ORDER" |
| 61 |
.PP |
| 62 |
When constructing the method chain, method implementations are searched for in |
| 63 |
the following order: |
| 64 |
.IP [1] |
| 65 |
In the classes mixed into the object, in class traversal order. The list of |
| 66 |
mixins is checked in natural order. |
| 67 |
.IP [2] |
| 68 |
In the classes mixed into the classes of the object, with sources of mixing in |
| 69 |
being searched in class traversal order. Within each class, the list of mixins |
| 70 |
is processed in natural order. |
| 71 |
.IP [3] |
| 72 |
In the object itself. |
| 73 |
.IP [4] |
| 74 |
In the object's class. |
| 75 |
.IP [5] |
| 76 |
In the superclasses of the class, following each superclass in a depth-first |
| 77 |
fashion in the natural order of the superclass list. |
| 78 |
.PP |
| 79 |
Any particular method implementation always comes as \fIlate\fR in the |
| 80 |
resulting list of implementations as possible; this means that if some class, |
| 81 |
A, is both mixed into a class, B, and is also a superclass of B, the instances |
| 82 |
of B will always treat A as a superclass from the perspective of inheritance. |
| 83 |
This is true even when the multiple inheritance is processed indirectly. |
| 84 |
.SS FILTERS |
| 85 |
.PP |
| 86 |
When an object has a list of filter names set upon it, or is an instance of a |
| 87 |
class (or has mixed in a class) that has a list of filter names set upon it, |
| 88 |
before every invocation of any method the filters are processed. Filter |
| 89 |
implementations are found in class traversal order, as are the lists of filter |
| 90 |
names (each of which is traversed in natural list order). Explicitly invoking |
| 91 |
a method used as a filter will cause that method to be invoked twice, once as |
| 92 |
a filter and once as a normal method. |
| 93 |
.PP |
| 94 |
Each filter should decide for itself whether to permit the execution to go |
| 95 |
forward to the proper implementation of the method (which it does by invoking |
| 96 |
the \fBnext\fR command as filters are inserted into the front of the method |
| 97 |
call chain) and is responsible for returning the result of \fBnext\fR. |
| 98 |
.PP |
| 99 |
Filters are invoked when processing an invocation of the \fBunknown\fR |
| 100 |
method because of a failure to locate a method implementation, but \fInot\fR |
| 101 |
when invoking either constructors or destructors. (Note however that the |
| 102 |
\fBdestroy\fR method is a conventional method, and filters are invoked as |
| 103 |
normal when it is called.) |
| 104 |
.SH EXAMPLES |
| 105 |
.PP |
| 106 |
This example demonstrates how to use the \fBnext\fR command to call the |
| 107 |
(super)class's implementation of a method. The script: |
| 108 |
.PP |
| 109 |
.CS |
| 110 |
oo::class create theSuperclass { |
| 111 |
method example {args} { |
| 112 |
puts "in the superclass, args = $args" |
| 113 |
} |
| 114 |
} |
| 115 |
oo::class create theSubclass { |
| 116 |
superclass theSuperclass |
| 117 |
method example {args} { |
| 118 |
puts "before chaining from subclass, args = $args" |
| 119 |
\fBnext\fR a {*}$args b |
| 120 |
\fBnext\fR pureSynthesis |
| 121 |
puts "after chaining from subclass" |
| 122 |
} |
| 123 |
} |
| 124 |
theSubclass create obj |
| 125 |
oo::objdefine obj method example args { |
| 126 |
puts "per-object method, args = $args" |
| 127 |
\fBnext\fR x {*}$args y |
| 128 |
\fBnext\fR |
| 129 |
} |
| 130 |
obj example 1 2 3 |
| 131 |
.CE |
| 132 |
.PP |
| 133 |
prints the following: |
| 134 |
.PP |
| 135 |
.CS |
| 136 |
per-object method, args = 1 2 3 |
| 137 |
before chaining from subclass, args = x 1 2 3 y |
| 138 |
in the superclass, args = a x 1 2 3 y b |
| 139 |
in the superclass, args = pureSynthesis |
| 140 |
after chaining from subclass |
| 141 |
before chaining from subclass, args = |
| 142 |
in the superclass, args = a b |
| 143 |
in the superclass, args = pureSynthesis |
| 144 |
after chaining from subclass |
| 145 |
.CE |
| 146 |
.PP |
| 147 |
This example demonstrates how to build a simple cache class that applies |
| 148 |
memoization to all the method calls of the objects it is mixed into, and shows |
| 149 |
how it can make a difference to computation times: |
| 150 |
.PP |
| 151 |
.CS |
| 152 |
oo::class create cache { |
| 153 |
filter Memoize |
| 154 |
method Memoize args { |
| 155 |
\fI# Do not filter the core method implementations\fR |
| 156 |
if {[lindex [self target] 0] eq "::oo::object"} { |
| 157 |
return [\fBnext\fR {*}$args] |
| 158 |
} |
| 159 |
|
| 160 |
\fI# Check if the value is already in the cache\fR |
| 161 |
my variable ValueCache |
| 162 |
set key [self target],$args |
| 163 |
if {[info exist ValueCache($key)]} { |
| 164 |
return $ValueCache($key) |
| 165 |
} |
| 166 |
|
| 167 |
\fI# Compute value, insert into cache, and return it\fR |
| 168 |
return [set ValueCache($key) [\fBnext\fR {*}$args]] |
| 169 |
} |
| 170 |
method flushCache {} { |
| 171 |
my variable ValueCache |
| 172 |
unset ValueCache |
| 173 |
\fI# Skip the caching\fR |
| 174 |
return -level 2 "" |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
oo::object create demo |
| 179 |
oo::objdefine demo { |
| 180 |
mixin cache |
| 181 |
method compute {a b c} { |
| 182 |
after 3000 \fI;# Simulate deep thought\fR |
| 183 |
return [expr {$a + $b * $c}] |
| 184 |
} |
| 185 |
method compute2 {a b c} { |
| 186 |
after 3000 \fI;# Simulate deep thought\fR |
| 187 |
return [expr {$a * $b + $c}] |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
puts [demo compute 1 2 3] \fI\(-> prints "7" after delay\fR |
| 192 |
puts [demo compute2 4 5 6] \fI\(-> prints "26" after delay\fR |
| 193 |
puts [demo compute 1 2 3] \fI\(-> prints "7" instantly\fR |
| 194 |
puts [demo compute2 4 5 6] \fI\(-> prints "26" instantly\fR |
| 195 |
puts [demo compute 4 5 6] \fI\(-> prints "34" after delay\fR |
| 196 |
puts [demo compute 4 5 6] \fI\(-> prints "34" instantly\fR |
| 197 |
puts [demo compute 1 2 3] \fI\(-> prints "7" instantly\fR |
| 198 |
demo flushCache |
| 199 |
puts [demo compute 1 2 3] \fI\(-> prints "7" after delay\fR |
| 200 |
.CE |
| 201 |
.SH "SEE ALSO" |
| 202 |
oo::class(n), oo::define(n), oo::object(n), self(n) |
| 203 |
.SH KEYWORDS |
| 204 |
call, method, method chain |
| 205 |
.\" Local variables: |
| 206 |
.\" mode: nroff |
| 207 |
.\" fill-column: 78 |
| 208 |
.\" End: |