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,
arg
refers to the table containing all the command-line arguments passed to a script. For instance, say I had a script calledtest.lua
that 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,
arg
is a member of the global environment table,_ENV
; thusarg
is equivalent to_ENV.arg
or_ENV["arg"]
.In Lua 5.3, it seems that
arg
as 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 variablearg
within functions in older version of Lua.