## Tcl9.0.3/Tk9.0.3 Documentation > Tcl Commands > next

### Tcl/Tk Applications | Tcl Commands | Tk Commands | [incr Tcl] Package Commands | SQLite3 Package Commands | TDBC Package Commands | tdbc::mysql Package Commands | tdbc::odbc Package Commands | tdbc::postgres Package Commands | tdbc::sqlite3 Package Commands | Thread Package Commands | Tcl C API | Tk C API | [incr Tcl] Package C API | TDBC Package C API

### NAME

next, nextto — 上位の級の手段の実装を起動する Invoke superclass method implementations

### 構文 SYNOPSIS

package require tcl::oo
next ?arg ...?
nextto class ?arg ...?

### 説明 DESCRIPTION

この next 命令は級か､上位級か､現在の手段によって上書きされた混合系､によって手段の実装の呼出しに使われる｡それは手段内でのみ使われる｡それは又篩内で篩が実際の実装を呼出した位置を指し示す為に使われる(櫛は連鎖に沿って進まないと決定されているかも知れず､選択した様に手段の連鎖に従って進む結果に処理されるかもしれない)｡ next 命令の結果は手段連鎖内の次の手段の結果である; もし手段連鎖内に更なる手段がない場合､ next の結果は不都合になる｡ next への引数 arg は､連鎖内の次の手段に渡す為の引数である｡ The next 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 next 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 next will be an error. The arguments, arg, to next are the arguments to pass to the next method in the chain.

nextto は､現在の手段連鎖についての実装( info object call を参照されたい)が使用されている級を識別する追加的引数をとることを除いて next と同じである; 選択された手段の実装は与えられた級により提供され､それは実在する非篩の起動を参照しそれは現在の実装でなく連鎖の上にある｡ The nextto command is the same as the next command, except that it takes an additional class argument that identifies a class whose implementation of the current method chain (see info object call) 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.

### 手段の連鎖 THE METHOD CHAIN

対象の手段が起動された時､幾つかの段階で事が生じる: When a method of an object is invoked, things happen in several stages:

1. 対象､その級､上位級､櫛､混入の構造は手段の連鎖を建設する為に調べられる､連鎖は起動すべき手段実装のリストを含む｡ The structure of the object, its class, superclasses, filters, and mixins, are examined to build a method chain, which contains a list of method implementations to invoke.

2. 連鎖上の最初の手段の実装が起動される｡ The first method implementation on the chain is invoked.

3. 手段実装が next 命令を(next に渡された引数と共に)起動する場合､その次の手段実装が起動される｡ If that method implementation invokes the next command, the next method implementation is invoked (with its arguments being those that were passed to next).

4. 全体としての手段呼出しからの結果は最外側の手段実装からの結果である; 内側の手段実装はその結果を next を通して返す｡ The result from the overall method call is the result from the outermost method implementation; inner method implementations return their results through next.

5. 手段の連鎖は次の使用の為に高速記憶域に貯蔵される｡ The method chain is cached for future use.

#### 手段捜索順序 METHOD SEARCH ORDER

手段連鎖を構築する時､手段実装は次の順序で捜索される: When constructing the method chain, method implementations are searched for in the following order:

1. 対象に混入された級､級の横断の順序で｡混入したリストは自然の順に検査される｡ In the classes mixed into the object, in class traversal order. The list of mixins is checked in natural order.

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.

3. 対象そのものの中｡ In the object itself.

4. 対象そのものの中｡ In the object's class.

5. 級の上位の級の中､自然な順序の上位級のリストに深い方が先の様式でそれぞれの上位の級が続く｡ In the superclasses of the class, following each superclass in a depth-first fashion in the natural order of the superclass list.

どの特定の手段実装も常に､実装の結果のリストに可能な限り遅く届く; これは､ある級 A が共に級 B に混入され､ B の上位級である場合､ B の実現値は､階層構造の見地から常に上位級として A を扱うことを意味する｡これは多段階層構造が間接的に処理されている時でさえも成立する｡ Any particular method implementation always comes as late 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.

#### 櫛 FILTERS

対象が櫛の名前のリストをその上に設定させるか､又は級の実現値(又は､一つの級に混合させている)であるとき､その櫛が処理したどの手段の全ての発動よりも前に､それは櫛の名前のリストを設定する｡篩の実装は､篩の名前のリストとしての級の横断順序である(それぞれは自然なリスト順に横断される)｡篩の実装は､篩の名前のリストとしての級の横断順序である(それぞれは自然なリスト順に横断される)｡明示的な篩としての手段の起動は２度の手段の起動を惹き起こす､一度は篩として一度は通常の手段として｡ 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.

それぞれの篩は自己の為に､手段の適切な実装に進む実行を許容するかを定め(それは手段呼出し連鎖の前面に挿入された篩として next 命令を起動することにより実行する)､ next の結果の返しに責務を負う｡ 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 next command as filters are inserted into the front of the method call chain) and is responsible for returning the result of next.

手動実装の配置の失敗の為に､unknown 手段の起動を処理している時は櫛が起動されるが､構成子か破壊子のどちらかを起動している時は､起動されない｡(しかし､destroy 手段は常套的な手段であり､それが呼ばれた時に櫛は通常のものとして起動される) Filters are invoked when processing an invocation of the unknown method because of a failure to locate a method implementation, but not when invoking either constructors or destructors. (Note however that the destroy method is a conventional method, and filters are invoked as normal when it is called.)

### 用例 EXAMPLES

この用例は､手段の(上位の)級の実装を呼ぶ為に next 命令を如何に使うかの例示である｡ 記述譜は: This example demonstrates how to use the next command to call the (super)class's implementation of a method. The script:

    oo::class create theSuperclass {
        method example {args} {
            puts "in the superclass, args = $args"
        }
    }

    oo::class create theSubclass {
        superclass theSuperclass
        method example {args} {
            puts "before chaining from subclass, args = $args"
            next a {*}$args b
            next pureSynthesis
            puts "after chaining from subclass"
        }
    }

    theSubclass create obj
    oo::objdefine obj method example args {
        puts "per-object method, args = $args"
        next x {*}$args y
        next
    }
    obj example 1 2 3

次のように印刷する: prints the following:

    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

この例は簡単な高速記憶級の構築であって､混合している対象の呼出しの手段全てへの臨時記憶を適用している､又それが計算時間をどう変えるかを示す: 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:

    oo::class create cache {
        filter Memoize
        method Memoize args {
            # 中核手段実装は篩わない
            # Do not filter the core method implementations
            if {[lindex [self target] 0] eq "::oo::object"} {
                return [next {*}$args]
            }

            # 値が既に高速域に記憶されていないか確認する
            # Check if the value is already in the cache
            my variable ValueCache
            set key [self target],$args
            if {[info exist ValueCache($key)]} {
                return $ValueCache($key)
            }

            # 値を計算し高速域に記憶して帰る
            # Compute value, insert into cache, and return it
            return [set ValueCache($key) [next {*}$args]]
        }

        method flushCache {} {
            my variable ValueCache
            unset ValueCache
            # 高速記憶を避ける
            # Skip the caching
            return -level 2 ""
        }
    }

    oo::object create demo
    oo::objdefine demo {
        mixin cache

        method compute {a b c} {
            after 3000 ;# Simulate deep thought
            return [expr {$a + $b * $c}]
        }

        method compute2 {a b c} {
            after 3000 ;# Simulate deep thought
            return [expr {$a * $b + $c}]
        }
    }

    puts [demo compute  1 2 3]      → prints "7" after delay
    puts [demo compute2 4 5 6]      → prints "26" after delay
    puts [demo compute  1 2 3]      → prints "7" instantly
    puts [demo compute2 4 5 6]      → prints "26" instantly
    puts [demo compute  4 5 6]      → prints "34" after delay
    puts [demo compute  4 5 6]      → prints "34" instantly
    puts [demo compute  1 2 3]      → prints "7" instantly
    demo flushCache
    puts [demo compute  1 2 3]      → prints "7" after delay

### SEE ALSO

oo::class, oo::define, oo::object, self

### KEYWORDS

call, method, method chain
Copyright © 2007 Donal K. Fellows