• Coroutine problem

    From Simon Geard@21:1/5 to All on Sat Jul 9 12:44:12 2022
    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!

    Simon

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Gollwitzer@21:1/5 to All on Sat Jul 9 15:25:04 2022
    Am 09.07.22 um 15:18 schrieb Christian Gollwitzer:

    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.


    Found the error: The "exit 0" in the original code terminates tkcon,
    therefore everything works as expected using this code! There is another (potential) problem only: After the "yieldto return" the coroutine still
    exists and must be executed one more time in order to be destroyed. THis
    might introduce memory leaks, depending on the usage.

    Christan

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Gollwitzer@21:1/5 to All on Sat Jul 9 15:18:39 2022
    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)
  • From Simon Geard@21:1/5 to Christian Gollwitzer on Sat Jul 9 18:12:37 2022
    On 09/07/2022 14:18, Christian Gollwitzer wrote:
    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

    Thank you. yieldto worked perfectly.

    Simon

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)