8
function tell(num,...)
    print("value of implicit table:",arg)
    --print("value of implicit table:",...)
    select(1,arg)
    --select(1,...)
end
tell(12,43,12,55)

Why is it that using ... in an expression causes the value of arg to
be nil e.g. with print("value of implicit table:",...) or select(1,...)?

1 Answer 1

14

Lua 5.1 officially deprecates the use of the arg table for varargs, preferring .... However, there is a compile time option for Lua itself, LUA_COMPAT_VARARG, to permit the use of arg in 5.1 code.

If LUA_COMPAT_VARARG was defined when Lua was compiled, an arg table will be created in varargs functions, and populated with the arguments - unless the compiler detects the use of ... inside the function. In that case, it assumes that you're using 5.1 style varargs instead of 5.0, and doesn't create the table. It does, however, still create the local named arg!

The upshot of this is that if LUA_COMPAT_VARARG is defined, vararg functions that don't use ... in the body get a local arg containing the argument list, and vararg functions that do get a local arg containing nil. This bug is present in all versions of 5.1 and means, in particular, that you can't access a global or upvalue named arg from any varargs functions if LUA_COMPAT_VARARG was defined at compile time.

Lua 5.2 drops support for arg-style varargs entirely and thus does not have this issue regardless of how it was configured at compile time.

(Source: the changes in varargs handling between 5.0 and 5.1, and the LUA_COMPAT_VARARG option, are mentioned in the Lua 5.1 reference manual, section 7.1. The manual refers you to luaconf.h. The exact behaviour is not documented anywhere, as far as I'm aware of; it can be determined experimentally, by reading lparser.c and ldo.c, or from the posts on the mailing list that originally reported this issue.)

4
  • Okay, that's interesting. I've tried it with lua 5.2.0 and got this : table: 00899618 as args value(confirms what you said). By "compile time", do you mean when compiling the lua interpreter or when compiling a lua script using luac.exe. How does one get to know about such details?
    – Plakhoy
    Sep 8, 2013 at 1:07
  • @Segfault he means when the interpreter is built from source and something like -DLUA_COMPAT_VARARG is provided to the C compiler etc.
    – greatwolf
    Sep 8, 2013 at 3:15
  • -DLUA_COMPAT_ALL is in the makefile. I guess the _ALL means LUA_COMPAT_VARARG is included. Is this information in the documentation or something?
    – Plakhoy
    Sep 8, 2013 at 5:18
  • @Segfault it's documented-ish - the manual and the comments in luaconf.h mention it, the exact specifics of the behaviour (e.g. the way it creates a local named arg even in ... mode) are not documented anywhere that I'm aware of. I've edited the question to include links to the relevant sources. As for LUA_COMPAT_ALL, that's specific to 5.2 and enables all backwards-compatibility options when defined.
    – ToxicFrog
    Sep 8, 2013 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.