Parameter implicit “arg” in functions not work in

2020-04-07 19:35发布

问题:

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.

回答1:

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.



标签: lua