Page 1 of 1

[SOLVED] What does ... mean?

Posted: Sun Aug 17, 2014 11:34 pm
by CrackedP0t
I've seen ... in code samples and such, but what is it?

Re: What does ... mean?

Posted: Sun Aug 17, 2014 11:58 pm
by veethree
It means a function has an unknown amount of arguments. All the arguments you give the function are stored in a hidden table called arg.

Code: Select all

function foo(...)

Re: What does ... mean?

Posted: Mon Aug 18, 2014 1:02 am
by CrackedP0t
Thanks!

Re: What does ... mean?

Posted: Mon Aug 18, 2014 1:13 am
by slime
veethree wrote:All the arguments you give the function are stored in a hidden table called arg.
This part isn't correct. It used to be with Lua 5.0, which the official online version of the Programming in Lua book is based on, but since Lua 5.1 (which was released in 2006 and is what LuaJIT is based on) it's no longer the case.

You can assign ... to a new local table, or access individual parts with local variables, or use the select function. Here are some examples:

Code: Select all

function foo(...)
    local args = {...}
    local arg1, arg2 = ...
    local arg3, arg4, arg5 = select(3, ...)

    local argcount = select("#", ...)

    assert(args[1] == arg1)
    assert(args[4] == arg4)
end

foo("one", "two", "three", "four", "five", "six")
... can also be used after one or more named arguments in a function, like this:

Code: Select all

function bar(x, y, ...)
    print("bar", x, y, ...)
end

bar(100, 100, os.date())

Re: [SOLVED] What does ... mean?

Posted: Mon Aug 18, 2014 4:37 pm
by veethree
Does the old method work? I could have sworn i did something with this recently.

Re: [SOLVED] What does ... mean?

Posted: Mon Aug 18, 2014 4:42 pm
by slime
Not in LuaJIT.

There is an 'arg' table in love, but it's global and it contains the command-line arguments passed to the program when it was launched - just like the standalone Lua and LuaJIT interpreter programs.

Re: [SOLVED] What does ... mean?

Posted: Thu Aug 21, 2014 8:47 pm
by MarekkPie
The way I use varargs is often for passing arguments along to an unknown, inner function. For example, I often write an EntityManager class that helps with mapping a function across many tables:

Code: Select all

function EntityManager:map(f, ...)
    for _,v in pairs(self._entities) do
        -- If the entity has the function f, then call it with the given vararg
        if v[f] then v[f](v, ...) end
    end
end

function love.update(dt)
    self.entityManager:map('update', dt)
end

function love.draw()
    self.entityManager:map('draw')
end

function love.keypressed(key, code)
    self.entityManager:map('keypressed', key, code)
end

Re: [SOLVED] What does ... mean?

Posted: Fri Aug 22, 2014 12:32 am
by davisdude
This is what I do:

Code: Select all

local function CheckUserdata( ... )
	local Userdata = {}
	if type( ... ) ~= 'table' then Userdata = { ... } else Userdata = ... end
	return Userdata
end
Then, do this:

Code: Select all

function Blah( ... )
    local BlahTable = CheckUserdata( ... )
end