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