The second talk in Miguel Sofer's NRE series at Tcl2008, following up on NRE: the non-recursive engine in Tcl 8.6:
Coroutines--what?
TIP #328 [L1 ] specifies three new commands: coroutine, yield, and info coroutine. [coroutine] is evaluated equivalently to uplevel #0 [list cmd ?arg ...?]. [yield] suspends operation. Walked through an example from the tip.
[yield] returns twice: when yielding (caller reeives a return value from the yielding coroutine); when resuming coroutine sees the [yield] command returning. [cmdName] is "garbage collected". Example at http://msofer.com:8080/wiki?name=Coroutines
Went through the wiki page Coroutines for event-based programming showing converting a console application to a GUI.
Coroutines: why?
Wil need to learn from experience which is better/faster (event loop vs dispatcher)
Coroutines: tech?
The byte code engine doesn't know much about coroutines; things happen behind its back (that's mostly true).
Deleting a suspended coroutine winds-down the suspended execution (as if interp's limit had been reached).
Special considerations:
Tailcalls: what?
(...Due to time constraints a couple slides were on screen for only a few seconds each...)
A pseudo-implementation (may be useful for debugging purposes: stack trace!):
proc tailcall args { set cmd [[uplevel 1 [[\ list namespace which [[lindex $args 0]]]]]] lset args 0 $cmd uplevel 2 $args }
Tailcalls: why?
Tailcalls: tech: tailcall arranges for the command to be run right after all callbacks that were scheduled by the callee's implementation.
Question from DKF: What's next Miguel? Answer: coffee?
Question from DGP about whether coroutine resume command name could be pre-resolved with [namespace origin]. Answer: great feature request. DGP I thought more about this, and on deeper reflection, it's not obviously true this is a good idea. It comes down to subtle distinctions about exactly what timing of command resolution is desired, and whether we want to foreclose any of the possible choices.
Question from the chat about heap memory usage. (Some KB; didn't catch how many if coroutines used at all today; will be less before 8.6.0 is released).
DKF: I double-checked this. It's 8kB, which is far too hungry. But to reiterate, this is untuned.
MS: I just thinned them down 10x, they are below 1kB now. Further tuning may yet occur ...