lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On 03/06/2011 23.59, Leo Razoumov wrote:
On Fri, Jun 3, 2011 at 06:47, steve donovan<>  wrote:
On Fri, Jun 3, 2011 at 12:25 PM, Dirk Laurie<>  wrote:
Which is better?

I would say: avoid the local 'arg' like the plague, it's a Lua 5.0 -ism ;)

I aways wondered how to get arg[0] (name of the current script) with {...}?

The following script show the difference

-- test.lua (Lua-5.1)
print("arg[0]", '=',  arg[0])
print("({...})[0]", '=', ({...})[0])
   arg[0]  =       test.lua
   ({...})[0]      =       nil


If I'm not mistaken, arg for main chunks is not being deprecated. It is a feature of the standalone interpreter, not of the core language, and I think it is not going to be removed with 5.2. Therefore arg[0] will still be the right way to get the script name in 5.2 too.

The deprecated 'arg' is only the 5.0 way of getting the vararg arguments, which now is accomplished using '...'. Thus it is only 'arg' at function level that will be removed from 5.2.

See the following test code:

-- test.lua
local function f(...)
  return arg[0], #arg, arg[1], arg[2]

print( arg[0] )
print( f( 10, 'a' ) )

-- Output (5.1.4):
nil   2   10   a

-- Output (5.2 alpha):
test.lua   0   nil   nil

-- Lorenzo