Index: TODO =================================================================== diff -u -r3397af1d1bd4eee19e5a2d059e54fc98195b1cc8 -r6cbb838340f985cc250def919416069f3d43e0df --- TODO (.../TODO) (revision 3397af1d1bd4eee19e5a2d059e54fc98195b1cc8) +++ TODO (.../TODO) (revision 6cbb838340f985cc250def919416069f3d43e0df) @@ -3945,6 +3945,9 @@ - extended regression tests for yield - implemented "next" for ruby-like enumerators (each needs still more work) +- tcl86.test: better implementation of method "each", + cleanup and extension of enumerator tests + ======================================================================== TODO: Index: tests/tcl86.test =================================================================== diff -u -r3397af1d1bd4eee19e5a2d059e54fc98195b1cc8 -r6cbb838340f985cc250def919416069f3d43e0df --- tests/tcl86.test (.../tcl86.test) (revision 3397af1d1bd4eee19e5a2d059e54fc98195b1cc8) +++ tests/tcl86.test (.../tcl86.test) (revision 6cbb838340f985cc250def919416069f3d43e0df) @@ -116,11 +116,8 @@ # :public method each {var body} { while 1 { - set value [:next] - #[self] apply [list $var $body] $value - :apply [list $var $body] $value - #uplevel [list [self] apply [list $var $body] $value] - #uplevel [list [self] apply [list $var $body] $value] + uplevel [list set $var [:next]] + uplevel $body } } @@ -144,23 +141,10 @@ nx::Class create Enumerator -superclass Yielder { :property members:0..n :property {block { - foreach m ${:members} { - yield $m - } + foreach m ${:members} { yield $m } }} } - puts stderr ===0 - set e [Enumerator new -members {1 2 3}] - while 1 { - puts stderr [$e next] - } - - puts stderr ===1 - set e [Enumerator new -members {a be bu}] - $e each x {puts x=$x} - puts stderr ===2 - # # Some application class using the enumerator (just used for easy # testing) @@ -181,22 +165,45 @@ # test Enumerator.each :public method concat {} { - set ::string "-" + set string "-" + set i 0 set e [Enumerator new -members {a be bu}] - # For the time being, we seem just able to address gobal - # variables in the each body. TODO: this is not desired. - $e each x { - append ::string $x- - } - #puts stderr vars=[:info vars] - return $::string + $e each x { append string $x-([incr i])- } + return $string } :create f1 } ? {f1 sum} 6 - ? {f1 concat} "-a-be-bu-" + ? {f1 concat} "-a-(1)-be-(2)-bu-(3)-" + + + # + # Define a class ATeam that uses "Enumerator", refines the method + # "each" and adds another method "concat" + # + nx::Class create ATeam -superclass Enumerator { + # + # Overload "each" to show overloading. Here, we simply capitalize + # the memebers in the "each" method. + # + :public method each {var body} { + while 1 { + set value [string totitle [:next]] + uplevel [list set $var $value] + uplevel $body + } + } + # Define some arbitrary method using ATeam.each + :public method concat {} { + set string "-" + :each x { append string $x- } + return $string + } + } + ATeam create a1 -members {alice bob ceasar} + ? {a1 concat } "-Alice-Bob-Ceasar-" } #