为什么在一个函数的前pression使用......导致arg的值是零在Lua? [英] Why does the use of ... in any expression in a function cause the value of arg to be nil in Lua?

查看:261
本文介绍了为什么在一个函数的前pression使用......导致arg的值是零在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)

为什么在一个前pression使用 ... 引起 ARG 的价值结果
例如与打印(隐含表的价值:,...)选择(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正式去precates为可变参数使用 ARG 表,preferring ... 。然而,为lua本身就是一个编译时间选项, LUA_COMPAT_VARARG ,允许在5.1 $ C使用 ARG 的$ C。

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_COMPAT_VARARG 当Lua编译,一个 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 是指不使用,可变参数的函数 ... 在身体得到一个本地ARG 包含参数列表,可变参数函数的的获得本地ARG 包含。此错误是在5.1和手段所有版本present,特别是,你不能访问一个全局或<$的upvalue名为 ARG 从任何可变参数的函数C $ C> 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.

(来源://www.lua:在可变参数5.0和5.1之间处理的变化,而 LUA_COMPAT_VARARG 选项中的 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.)

这篇关于为什么在一个函数的前pression使用......导致arg的值是零在Lua?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆