TIP #372: Multi-argument Yield for Coroutines

Title:Multi-argument Yield for Coroutines
Version:$Revision: 1.3 $
Author:Colin McCormack <colin at chinix dot com>
Created:Wednesday, 11 August 2010
Discussions To:http://wiki.tcl.tk/26006
Obsoleted-By:TIP #396
Keywords:coroutine, yield


This TIP proposes a command that allows a coroutine to accept multiple arguments being passed in on return from yield.


The coroutine mechanism of TIP #328 has given Tcl 8.6 a new primitive which permits light-weight mulitasking. The mechanism as specified by TIP #328 lacks the ability to emulate general-purpose procedural invocation through the arbitrary limitation of coroutine calls to a single argument (except on the initial call).

The ability of coroutines to emulate general-purpose procedural invocation is of great utility, the lack of which leads to stilted verbiage, and diverges unnecessarily from Tcl's other functionals.

The yieldm command, as currently implemented by ::tcl::unsupported::yieldm, extends coroutines to permit general emulation, and it is the proposal of this TIP that yieldm be moved to the global namespace, or such other namespaces as may be used for coroutine functionality in the future.

Syntax and Semantics of yieldm

yieldm ?value?

This is just like yield, except that it causes the coroutine command to accept arbitrarily many arguments and will return the list of those arguments (i.e., an empty list if there are no arguments, a single-element list if there is one, a pair for two, etc.)

Detailed Rationale


Restatement-in-summary of the arguments in http://wiki.tcl.tk/26006:

  1. Coroutines should be able to simulate any command, not just any single-arg'd command. [Argument: generality]

  2. To implement single-arg's coroutines in multi-arg'd coroutines is trivial - nothing needs to be done. The converse (implementing multi-arg'd coroutines under coroutine is inefficient and difficult. [Argument: increased expressive power]

  3. There is no sound reason that the invocation of a coroutine should not resemble that of any other command. [Argument: principle of minimal surprise]

  4. It is impossible to construct forms like interp alias to address a coroutine in the common case that the caller is expecting to pass more than one argument.


Restatement-in-summary of the arguments in http://wiki.tcl.tk/26056:

The following commands create new commands: proc, coroutine, namespace ensemble, class create, $cls create, interp alias, interp create, ::thread::create, apply.

In each case except coroutine, the created command permits multiple arguments (arguably this being a key feature of Tcl in practice). There is no sound reason for this disparity, and for consistency the disparity should be removed, which the proposed yieldm does.


Simply rename the ::tcl::unsupported::yieldm into the global namespace, and extend the coroutine documentation to refer to the new command.


This document has been placed in the public domain.

Powered by Tcl[Index] [History] [HTML Format] [Source Format] [LaTeX Format] [Text Format] [XML Format] [*roff Format (experimental)] [RTF Format (experimental)]

TIP AutoGenerator - written by Donal K. Fellows