lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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);
coroutine.resume(co);


-- 
"No one knows true heroes, for they speak not of their greatness." -- 
Daniel Remar.
Björn De Meyer 
bjorn.demeyer@pandora.be