Reputation: 512
I was try to use coroutine in lua, I tried the code below, repl.it here https://repl.it/repls/WordyWonderfulVisitor and it does not print the list content in the loop.
local list = {1,2,3};
local function iter()
for i, v in ipairs(list) do
print(i, v)
coroutine.yield();
end
end
local co = coroutine.create(iter);
coroutine.resume(co);
coroutine.resume(co);
-- iter();
what's wrong with my code ?
Upvotes: 1
Views: 783
Reputation: 26754
There is nothing wrong with your code. It prints 1 1
and 2 2
as expected and the results are the same in Lua 5.1-5.4 versions.
If you want to see one more result 3 3
, then you need to call resume one more time. You can also check the status of the coroutine by using coroutine.status
, so after the first two executions you'll get "suspended"
and after the execution is completed you'll get "dead"
.
Upvotes: 2