Return or return

I am learning Lua coroutines. I found a tied thing for me, both

meta = function () for i = 1, 10 do coroutine.yield(i) end end for i in coroutine.wrap(function() return meta() end) do print(i) end 

and

 meta = function () for i = 1, 10 do coroutine.yield(i) end end for i in coroutine.wrap(function() meta() end) do print(i) end 

(note that there is a refund in the first version) give me

  ~ / test% lua t.lua
 1
 2
 3
 4
 5
 6
 7
 eight
 nine
 ten

So what is the role of return ? I think meta() will return a value, and an anonymous function will also return it. So why is an anonymous function without return also correct?

+4
source share
1 answer

No, meta returns nothing - at least nothing important.

The output passed to variable i outer loop comes from the yield method, and not from the return.

This can be seen if you write the loop as follows:

 for i in coroutine.wrap(function() val = {meta()} print ("----") print (val) end) do print(i) end 

Output signal

 1 2 3 4 5 6 7 8 9 10 ---- nil 

An anometric function, as well as meta is called only once.

+1
source

All Articles