How are coroutines implemented in smalltalk?

Is it possible to implement coroutines in smalltalk?

If your answer is no: why not?

Or, if yes, can you give me an example?

+4
source share
1 answer

Most Smalltalk have methods for manipulating the stack of thisContext. You can use them to implement coroutines, although working with the stack at this level can be a bit tedious.

GNU Smalltalk and the latest versions of Squeak and Pharo also offer the Generator class, which makes it easy to write generators (that is, coroutine types that give multiple values):

"This generator yield an infinite sequence of 1" generator := Generator on: [ :gen | [ gen yield: 1 ] repeat ]. (1 to: 100) do: [:i | Transcript show: (generator next printString); cr] 
+8
source

All Articles