• Reason why yield takes an argument and yieldto a list

    From Harald Oehlmann@[email protected] to comp.lang.tcl on Fri Sep 12 11:02:59 2025
    From Newsgroup: comp.lang.tcl

    Beginner in coroutines, sorry.

    Is there a rationale, why yield call takes one argument and yieldto
    takes multiple arguments?

    proc cp {} {
    puts "<[yield y1]>"
    puts "<[yieldto string cat y2]>"
    return
    }
    % coroutine ct cp
    y1
    % ct "in 1"
    <in 1>
    y2
    % ct "in 2" "in 3"
    <{in 2} {in 3}>

    On the outside (e.g. usage side) of the coroutine, I find this quite
    strange, that once, only one argument is permitted, while otherwise,
    there are multiple arguments. Why isn't this the same for yield and yieldto?

    Thanks for all,
    Harald
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Ashok@[email protected] to comp.lang.tcl on Fri Sep 12 16:01:09 2025
    From Newsgroup: comp.lang.tcl

    Despite the similar names, the two are different beasts.

    yield returns control back to the caller and is analogous to the return command and therefore takes a single argument (just as return does). The caller of a coroutine always expects a single value to be returned
    (which may of course be a list) just as the caller of a proc expects a
    single value to be returned.

    yieldto does not directly return to the caller. It invokes some other
    command that then returns to the caller. This is analogous to tailcall,
    not return. That called command may take any number of arguments and
    hence yieldto must take an arbitrary number of arguments that may need
    to be passed to that command. That called command will then return a
    single value to the original coroutine caller.

    /Ashok

    On 9/12/2025 2:32 PM, Harald Oehlmann wrote:
    Beginner in coroutines, sorry.

    Is there a rationale, why yield call takes one argument and yieldto
    takes multiple arguments?

    proc cp {} {
        puts "<[yield y1]>"
        puts "<[yieldto string cat y2]>"
        return
    }
    % coroutine ct cp
    y1
    % ct "in 1"
    <in 1>
    y2
    % ct "in 2" "in 3"
    <{in 2} {in 3}>

    On the outside (e.g. usage side) of the coroutine, I find this quite strange, that once, only one argument is permitted, while otherwise,
    there are multiple arguments. Why isn't this the same for yield and
    yieldto?

    Thanks for all,
    Harald

    --- Synchronet 3.21a-Linux NewsLink 1.2