[SOLVED] What does ... mean?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- CrackedP0t
- Citizen
- Posts: 69
- Joined: Wed May 07, 2014 4:01 am
- Contact:
[SOLVED] What does ... mean?
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. Ö
Here, have an umlaut. Ö
Re: What does ... mean?
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(...)
- CrackedP0t
- Citizen
- Posts: 69
- Joined: Wed May 07, 2014 4:01 am
- Contact:
- slime
- Solid Snayke
- Posts: 3166
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: What does ... mean?
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.veethree wrote:All the arguments you give the function are stored in a hidden table called arg.
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")
Code: Select all
function bar(x, y, ...)
print("bar", x, y, ...)
end
bar(100, 100, os.date())
Re: [SOLVED] What does ... mean?
Does the old method work? I could have sworn i did something with this recently.
- slime
- Solid Snayke
- Posts: 3166
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: [SOLVED] What does ... mean?
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.
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?
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?
This is what I do:
Then, do this:
Code: Select all
local function CheckUserdata( ... )
local Userdata = {}
if type( ... ) ~= 'table' then Userdata = { ... } else Userdata = ... end
return Userdata
end
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
Who is online
Users browsing this forum: Ahrefs [Bot] and 7 guests