Lua coroutine.yield(-1)的含义?

标签 lua coroutine

coroutine.yield(-1) 是什么意思?我不明白这里的-1

代码块和输出是:

> function odd(x)
>>   print('A: odd', x)
>>   coroutine.yield(x)
>>   print('B: odd', x)
>> end
>
> function even(x)
>>   print('C: even', x)
>>   if x==2 then return x end
>>   print('D: even ', x)
>> end
>
> co = coroutine.create(
>>   function (x)
>>     for i=1,x do
>>       if i==3 then coroutine.yield(-1) end
>>       if i % 2 == 0 then even(i) else odd(i) end
>>     end
>>   end)
>
> count = 1
> while coroutine.status(co) ~= 'dead' do
>>   print('----', count) ; count = count+1
>>   errorfree, value = coroutine.resume(co, 5)
>>   print('E: errorfree, value, status', errorfree, value, coroutine.status(co))
>> end
----    1
A: odd  1
E: errorfree, value, status     true    1       suspended
----    2
B: odd  1
C: even 2
E: errorfree, value, status     true    -1      suspended
----    3
A: odd  3
E: errorfree, value, status     true    3       suspended
----    4
B: odd  3
C: even 4
D: even         4
A: odd  5
E: errorfree, value, status     true    5       suspended
----    5
B: odd  5
E: errorfree, value, status     true    nil     dead
>

最佳答案

传递给相应coroutine.yield 的任何参数都由coroutine.resume 返回。所以这里的coroutine.yield(-1)中的-1没什么特别的,它类似于coroutine.yield(x)中的函数奇数(x)

counter2i3时执行。对应的输出为:

----    2
B: odd  1
C: even 2
E: errorfree, value, status     true    -1      suspended

在表示没有错误的ture之后,看到这里的-1了吗?这是调用 coroutine.yield(-1) 的值,它最终作为 coroutine.resume 的返回值。

同理,coroutine.resume的其他返回值为135 ,全部来自函数 odd(x) 中的 coroutine.yield(x)

关于Lua coroutine.yield(-1)的含义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20343097/

相关文章:

android - 如何检查物体是否在 corona sdk 中向下加速?

lua - 未找到 'bit' 的 LuaRocks 模块

android - 协程作业返回值

android - 从非生命周期类的主调度程序切换到 IO 调度程序

lua - 大 lua 数字打印不正确

colors - Lua:褪色函数

android - Kotlin协程,有没有更好的方法来返回这个值?

python-3.x - 如何装饰 asyncio.coroutine 以保留其 __name__?

lua - 字符串中字符的最后一个索引

c# - 怎么理解这个Unity Coroutine呢?