lua-users home
lua-l archive

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


On Oct 30, 2002 at 06:15 +0100, Benoit Germain wrote:
> 
> I have made some modifications to lua-5.0-alpha that enable a script
> to call a coroutine with different arguments at each frame. My use
> is the following: the coroutine body takes as arguments a system
> date and a time delta since the previous date it was
> called. Obviously these values change at each call, and I did not
> want the overhead of putting them in a table being the real (and
> constant) argument of the coroutine.
> 
> The changes are relatively small. Is there a possibility to have
> them added to the 5.0-beta release if I provide a suitable patch
> file ?

If I may butt in... this seems like useful functionality, but it
doesn't seem very "clean" to me, to modify existing parameters in a
stealthy way.  As an alternative, would it be possible to have yield()
return values explicitly, which are passed by the resume call?

E.g., modifying an ealier example you gave:

function f(a,b,c)
        while true
        do
                print ( a .. b .. c )
                a, b, c = coroutine . yield ( )
        end
end

c = coroutine . create (f)

c("lua ","is ","great!") -- call f() with these args
--> lua is great!
c("Alex Barros ", "is ", "great!") -- yield() inside f() returns the provided args
--> Alex Barros is great!
c() -- yield() inside f() returns no values
--> attempt to concat a nil value

It seems that this would probably be as efficient, and clearer in the
code, and certainly easier to explain in documentation.  This also
makes it possible for your coroutine function to call sub-functions
that call yield, and get the information you want.

-- 
Thatcher Ulrich
http://tulrich.com