I was using arg as an argument name for a function:
function foo(cmd, arg)
-- etc.
end
I just learned arg is a special, hidden variable that represents a table of arguments when using variable arguments:
function foo(bar, baz, ...)
-- `arg` now holds arguments 3 and up
end
Should I expect any issues with using arg as an argument name in my code?
Firstly, please note that I am using Lua 5.3 and this is the version I prefer. (Though, I imagine I prefer it simply because it is the one I started on and am most familiar with.)
Secondly, what version of Lua are you using? In Lua 5.3,
argrefers to the table containing all the command-line arguments passed to a script. For instance, say I had a script calledtest.luathat looked something like this:If I executed the script as
lua test.lua hello there, friend, it would produce the outputNote that in Lua 5.3,
argis a member of the global environment table,_ENV; thusargis equivalent to_ENV.argor_ENV["arg"].In Lua 5.3, it seems that
argas a marker of variadic arguments in a function has been depreciated. A simple, table-based solution exists though, as in the following example:The line
local args = {...}has the same behavior of the variableargwithin functions in older version of Lua.