lua-users home
lua-l archive

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


I have a question regarding the usage of the ellipsis ("...").  I am seeing some sort of conflict with the hidden "arg" variable and the ellipsis.  If I mix usage in a function I get bad results--arg goes nil.  I have successfully used this operator in the past but somehow avoided this particular use case.  I was wondering if this is a bug or expected behavior.  I am testing under Ubuntu with Lua v5.1.4 as well as an embedded ARM target running Lua v5.1.5.

This example works fine:
function foo( ... )
    print( "ARGS: ", arg )
    for k,v in pairs(arg) do
        print( k,v )
    end   
end
foo( "some", true, 1 )

OUTPUT:
ARGS:     table: 0x1750cb0
1    some
2    true
3    1
n    3

If I add a reference to ellipsis in foo() I get a nil for arg.  I can also move the offending line to the end of the function (or anywhere for that matter) and I get the nil arg error.
function foo( ... )
    local t = ...                      -- Can be anything: type(...), print(...)
    print( "ARGS: ", arg )
    for k,v in pairs(arg) do
        print( k,v )
    end   
end
foo( "some", true, 1 )

OUTPUT:
ARGS:     nil
lua: /home/tmoore/junk.lua:9: bad argument #1 to 'pairs' (table expected, got nil)
stack traceback:
    [C]: in function 'pairs'
    /home/tmoore/junk.lua:9: in function 'foo'
    /home/tmoore/junk.lua:13: in main chunk
    [C]: ?

Any insight would be appreciated.