Björn De Meyer | 1 Feb 2003 02:08
Picon

Re: Lua 5 migration

Joe Stewart wrote:
> 
> Coroutines look pretty neat. Any help understanding them besides the source
> code?

Well, think of coroutines as a way to be able 
to leave a function and return to it later at 
the exact same spot where you left it. 
It is especially useful for returning back to 
a long-running loop. It reduces the need for
call-back functions. I only recently discovered
how powerful coroutines are myself. 

A very simple example illustrates this:

function tryyield()
  print("First yield")
  coroutine.yield()
  print("Second yield")
  coroutine.yield()
  print("Third yield")
  coroutine.yield();
  print("All done");
end

co = coroutine.create(tryyield)

coroutine.resume(co);
coroutine.resume(co);
coroutine.resume(co);
(Continue reading)


Gmane