Replies: 2 comments 8 replies
-
I can think of one language that could benefit from a more coupled model: Rust. Its
If this sounds similar to JS's Edit: Rust is also looking into coroutines that work more or less the same way, and those are very likely to require returning values from suspends as well. Lowering that to underlying function returns will of course be simpler than using a global variable. |
Beta Was this translation helpful? Give feedback.
-
Again, a comparison with functions and calls/returns helps. Functions have parameters and results. Those do not replace more complex data structures in memory, but they provide the building blocks for realising them. Nobody would want to communicate everything through global state. The same holds for stacks and suspend/resume. The ability to put payload on a suspend/resume provides a building block for constructing more complex abstractions, like channels, which otherwise would have to be bootstrapped with global state in memory or globals. In both cases, functions and stacks, the ability to pass parameters is part of their structure, and avoids the need for global state where it doesn't belong. |
Beta Was this translation helpful? Give feedback.
-
Most of the designs that we have considered for stack switching have the feature that, along with switching computation between activities, we also 'communicate' some data.
The reason for this is a combination of two requirements: (a) it is often the case that data does need to be communicated (for example, when a generator yields, it also needs to 'return' the value it found to the generator's consumer; and (b) it is also often necessary to communicate the reason for the switch: in a suspend/resume pattern the yielder needs to communicate it's reason for suspending and, when a computation is resumed, some way of encoding the success or otherwise needs to be transmitted.
These are compelling reasons for simultaneously communicating some data along with the switch.
However, many, if not most, languages that offer coroutining capabilities separate these functions. For example, in go-lang, channels are used to communicate data. Furthermore, entangling them can make some run-time tasks more difficult. For example, in Swift, the starting and stopping of tasks is the responsibility of the scheduler; and the scheduler does not manage the data portion of the switch. One reason for this is that the types of data communicated are inherently private to the switching (sic) parties and the scheduler cannot be aware of it.
Separating the functionalities would undoubtedly enable a simpler design; at a potential cost of performance. Effectively, languages that use channels rely on storing results and tasks in memory during a stack switch.
Beta Was this translation helpful? Give feedback.
All reactions