[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: tinsert (was: Finding the version)
- From: Reuben Thomas <rrt@...>
- Date: Tue, 14 Aug 2001 12:28:07 +0100 (BST)
> > > Be careful: to correctly implement tinsert in Lua using the C tinsert you must pass the correct number of parameters to the C
> > > function, like in
> > >
> > > function tinsert(t, n, v)
> > > if (v) then
> > > %tinsert(t, n, v)
> > > else
> > > %tinsert(t, n)
> > > end
> > > end
> >
> > Sorry, I meant that. The version I posted was an incorrect "optimisation"!
>
> Even that is not correct. The original tinsert when called with a nil third
> argument will insert that nil into the table.
i.e. it will increment n. Hmm...
>
> Compatible (fixed) version would be:
>
> function tinsert(t, ...)
> if arg.n == 1 then
> %tinsert(t, arg[1])
> elseif arg.n == 2 then
I'd prefer
elseif arg.n >= 2 then
%tinsert(t, arg[1], arg[2])
elseif t then
%tinsert(t) -- let the implementation generate the error
else
%tinsert() -- let the C implementation generate the error
end
...which works fine with extra args, and lets the C implementation generate
the error for one or zero args. The difference between passing one and zero
args is perhaps a bit fussy, though.
--
http://sc3d.org/rrt/ | Travail broadens the behind