如何在 ActionScript 中生成 python/ruby/javascript 样式生成器?
我想在actionscript中使用协程来实现状态机。
我希望能够
function stateMachine():void
{
sendBytes(0xFFFF);
var receiveBytes:ByteArray = yield()
sendBytes(receiveBytes);
}
stateMachine.send( Socket.read() )
在 博客条目
I want to use coroutines in actionscript to implement a state machine.
I'd like to be able to do something like the following
function stateMachine():void
{
sendBytes(0xFFFF);
var receiveBytes:ByteArray = yield()
sendBytes(receiveBytes);
}
stateMachine.send( Socket.read() )
like in this blog entry
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,Actionscript 没有协程、延续或任何可以为您提供相关行为的东西(调用函数而不推送堆栈帧)。您可以使用静态变量和开关来伪造它,但这违背了使用协程作为状态机的目的。另外,没有尾调用(仍然只有 ECMASCRIPT 提案,如据我所知),伪造的协程不会像真正的协程那样使用恒定的堆栈空间。
关于您的示例代码,协程通常需要循环才能发挥作用。
As far as I know, Actionscript doesn't have coroutines, continuations or anything that will give you the relevant behavior (call a function without pushing a stack frame). You can fake it using static variables and a
switch
, but that defeats the purpose of using coroutines for state machines. Also, without tail calls (still only a proposal for ECMASCRIPT, as far as I know), faked coroutines won't use constant stack space as real coroutines do.Regarding your sample code, coroutines generally need to loop to be useful.
嗯,这个怎么样?
Well, how about this?