[SOLVED] What does ... mean?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
CrackedP0t
Citizen
Posts: 69
Joined: Wed May 07, 2014 4:01 am
Contact:

[SOLVED] What does ... mean?

Post by CrackedP0t »

I've seen ... in code samples and such, but what is it?
Last edited by CrackedP0t on Mon Aug 18, 2014 1:03 am, edited 1 time in total.
/人 ◕‿‿◕ 人\
Here, have an umlaut. Ö
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: What does ... mean?

Post 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(...)
User avatar
CrackedP0t
Citizen
Posts: 69
Joined: Wed May 07, 2014 4:01 am
Contact:

Re: What does ... mean?

Post by CrackedP0t »

Thanks!
/人 ◕‿‿◕ 人\
Here, have an umlaut. Ö
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: What does ... mean?

Post 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())
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

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

Post by veethree »

Does the old method work? I could have sworn i did something with this recently.
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

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

Post 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.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

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

Post 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
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

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

Post 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
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 13 guests