[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Vararg assignment thread?
- From: Andrew Starks <andrew.starks@...>
- Date: Sat, 30 Aug 2014 09:16:40 -0500
On Saturday, August 30, 2014, Petite Abeille <petite.abeille@gmail.com> wrote:
On Aug 30, 2014, at 6:03 AM, Andrew Starks <andrew.starks@trms.com> wrote:
> function o.method(self, fun, ...)
>
> success, ... = pcall(fun, ...)
> --vararg now return values of fun
> -etc.
>
> end
So, as Tim has pointed out, table.pack is what you want to make this work.
Something along these lines:
function Try( aFunction, ... )
local aResult = table.pack( pcall( aFunction, ... ) )
local ok = aResult[ 1 ]
if ok then
return table.unpack( aResult, 2, aResult.n )
end
end
If you feel adventurous, you could easily build a simple try/catch/finally mechanism out of it.
On the other hand, perhaps this was a purely rhetorical question?!?
Not rhetorical.
These answers were what I was hoping for.
I use tail calls and unpack, right now. Dirk's answer and the link are gold and explain my question nicely. As for use case:
I've made an "await" function that will await the completion of work from a thread, yielding to a parent thread if it isn't done. It's basically a non-blocking function call. On every check, I call another function that checks the first value (the flag) or if the coroutine is dead. If completed, it packs the values into a "return_values" table and returns that to the main loop, which unpacks the "return_values" table as the result.
It works fine. I was just thinking that it would be easier to reassign vararg and skip that function call and the temporary table.
The set stack space explains why it's not that simple. Thanks!
-Andrew