参数隐式"arg"被指定为0.在Lua中无法正常工作 [英] Parameter implicit "arg" in functions not work in Lua
问题描述
我在函数中使用隐式参数arg
时遇到问题.
该代码不起作用. http://www.lua.org/pil/5.2.html 文档,应该可以.
function listar_um (...)
for i,v in ipairs(arg) do
print("usando args " .. arg[i])
end
end
listar_um("Olá", 1, "Dois")
此代码与声明变量lista
一起使用.
function listar_um (...)
lista = {...}
for i,v in ipairs(lista) do
print("não usando args " .. lista[i])
end
end
listar_um("Olá", 1, "Dois")
为什么第一个示例不起作用?
测试脚本: http://www.codeshare.io/IPwRJ 执行在线脚本: http://www.compileonline.com/execute_lua_online.php >
谢谢.
PiL的第一版讨论Lua 5.0. arg
的使用在Lua 5.0中可用,而自Lua 5.1起已将其删除
您可以在 Lua 5.0参考手册中找到它,但不在 Lua 5.1参考手册中.
在线解释器使用的版本是Lua 5.2,您可以通过print(_VERSION)
来查找
编辑:经过一些测试,似乎arg
在Lua 5.1中仍然可用,但在Lua 5.2中不起作用.
I have a problem in use implicit parameter arg
in functions.
The code not works. The documentation, http://www.lua.org/pil/5.2.html, should works.
function listar_um (...)
for i,v in ipairs(arg) do
print("usando args " .. arg[i])
end
end
listar_um("Olá", 1, "Dois")
This code works with the declaring variable lista
.
function listar_um (...)
lista = {...}
for i,v in ipairs(lista) do
print("não usando args " .. lista[i])
end
end
listar_um("Olá", 1, "Dois")
Why the first example does not work?
Script for test: http://www.codeshare.io/IPwRJ Execute on-line script: http://www.compileonline.com/execute_lua_online.php
Thanks.
The first edition of PiL talks about Lua 5.0. The use of arg
is available in Lua 5.0, while it's removed since Lua 5.1
You can find it in Lua 5.0 reference manual, but not in Lua 5.1 reference manual.
The edition the online interpreter uses is Lua 5.2, you can find out by print(_VERSION)
.
Edit: after some tests, it seems that arg
is still available in Lua 5.1, but not working in Lua 5.2.
这篇关于参数隐式"arg"被指定为0.在Lua中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!