开发者

How are coroutines implemented in smalltalk?

Can I implement coroutines in smalltalk?

If your answer is 开发者_如何学Pythonno: why not?

Or if its yes: can you give me an example?


Most Smalltalk have stack manipulation methods on the thisContext object. You could use these to implement coroutines though dealing with the stack at this level may prove a bit tedious.

GNU Smalltalk and recent versions of Squeak and Pharo also offer a Generator class that makes it easy to write generators (i.e. types of coroutine that yield 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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜