[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Is it safe to unpack { nil, a, b } ?
- From: Niccolo Medici <niccolomedici@...>
- Date: Mon, 2 Nov 2015 22:36:11 +0200
On 11/2/15, Andrew Starks <andrew.starks@trms.com> wrote:
> On Mon, Nov 2, 2015 at 12:32 AM, Niccolo Medici <niccolomedici@gmail.com>
>> [...] I need to wrap one of the system functions. Such
>> functions return (nil, errmsg, errcode) on error. So I have the
>> following pattern in my code:
>>
>> local ret = { file:close() }
>> -- do something
>> return table.unpack(ret)
>> [...]
>
> I had the same issue and I did something similar. For me, this helped:
>
> ```
> local _unpack = table.unpack or unpack
> table.unpack = function(list, i, j)
> return _unpack(list,i or 1, j or list.n or nil)
> end
> ```
>
> I understand that this does not *solve* your problem, 100%. For me,
> this works
Thanks, Andrew. That's helpful. I arrived at a very similar solution
that satisfies me 100%:
function table.unpackx(t)
return table.unpack(t, 1, t.n or table.maxn(t))
end
(One also has to provide an implementation for maxn() because it was
deprecated after Lua 5.1.)