[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua 5 migration
- From: Björn De Meyer <bjorn.demeyer@...>
- Date: Sat, 01 Feb 2003 02:08:41 +0100
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