lua-users home
lua-l archive

varargs & table.insert()?

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


Hello,
The more I try to use varargs, the more confused I get :/
Considered the following scenario: one would like to insert an additional argument into a varargs...
For example:
local List = function( ... )
 return { n = select( '#', ... ), ... }
end
local aList = List( nil, 'a', nil )
for anIndex = 1, aList.n do
 print( '>', anIndex, aList[ anIndex ] )
end
> 1 nil
> 2 a
> 3 nil
Ok... 3 arguments... in the right order... now lets try to insert an additional value:
table.insert( aList, 'first' )
aList.n = aList.n + 1
> 1 first
> 2 a
> 3 nil
> 4 nil
Oh, no! 4 mangled arguments :(
Lets try something else:
aList = { n = aList.n + 1, 'first', unpack( aList, 1, aList.n ) }
> 1 first
> 2 nil
> 3 a
> 4 nil
Looks better... but only works when adding a leading argument...
So... how does one properly manipulate varargs constructs?!?!?
Thanks in advance.
Cheers,
PA.

AltStyle によって変換されたページ (->オリジナル) /