Am 09.07.22 um 13:44 schrieb Simon Geard:
I'm trying to use a coroutine to iterate over a list so that I get the
index as well:
proc list_sequencer {o} {
yield
set i 0
while {$i < [llength $o]} {
yield [list $i [lindex $o $i]]
incr i
}
return -code break
}
set tl {s i m p l e}
coroutine next_element list_sequencer $tl
while {1} {
lassign [next_element] idx e
puts "$idx -> $e"
}
exit 0
When I run it I get
0 -> s
1 -> i
2 -> m
3 -> p
4 -> l
5 -> e
invoked "break" outside of a loop
while executing
but don't understand why. Any help greatly appreciated!
I think you cannot yield an error code, that was missed out in the
original definition of yield. The manpage
https://www.tcl.tk/man/tcl/TclCmd/coroutine.html recommends to use
"yieldto return", and in fact this works:
proc list_sequencer {o} {
yield
set i 0
while {$i < [llength $o]} {
yield [list $i [lindex $o $i]]
incr i
}
yieldto return -code break -level 0
}
However, there is a strange side effect that if I run this in tkcon, the
breaks terminates the tkcon. Maybe some coro guru can comment on it.
Christian
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)