为什么在函数的任何表达式中使用 ... 会导致 arg 的值在 Lua 中为零? [英] Why does the use of ... in any expression in a function cause the value of arg to be nil in Lua?
问题描述
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)
为什么在表达式中使用...
会导致arg
的值变成
是 nil
例如with print("value of hidden table:",...)
或 select(1,...)
?
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,...)
?
推荐答案
Lua 5.1 正式弃用 arg
表作为可变参数,更喜欢 ...
.但是,Lua 本身有一个编译时选项,LUA_COMPAT_VARARG
,允许在 5.1 代码中使用 arg
.
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.
如果在编译 Lua 时定义了 LUA_COMPAT_VARARG
,将在 varargs 函数中创建一个 arg
表,并填充参数 - 除非编译器检测到使用 ...
在函数内部.在这种情况下,它假定您使用的是 5.1 样式的可变参数而不是 5.0,并且不会创建表.它确实,但是,仍然创建名为arg
的本地!
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
!
这样做的结果是,如果定义了 LUA_COMPAT_VARARG
,则在主体中不使用 ...
的可变参数函数会得到一个 local arg
包含参数列表,以及 do 获取包含 nil
的 local arg
的可变参数函数.此错误存在于 5.1 的所有版本中,尤其意味着如果 LUA_COMPAT_VARARG
定义在编译时间.
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 完全放弃了对 arg
样式的可变参数的支持,因此无论在编译时如何配置都不会出现这个问题.
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.
(来源:在 Lua 5.1 参考手册,第 7.1 节.该手册将您引向 luaconf.h.据我所知,确切的行为没有记录在任何地方;它可以通过阅读 lparser.c 和 ldo.c>,或来自最初报告此问题的邮件列表上的帖子.)
(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.)
这篇关于为什么在函数的任何表达式中使用 ... 会导致 arg 的值在 Lua 中为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!