Getting an argument to work in varag function in Lua 5.2 (integrated in Delphi)

When using the Lua 5.2 API, the code below prints "nil"

function __debug(szName, ...) print(type(arg)); end __debug("s", 1, 2, 3, 4); 

But this code works when using Lua 5.1 and prints a "table"

+4
source share
2 answers

If you reference the vararg function, the arg table was deprecated already in Lua 5.1 . In Lua 5.2, you can use table.pack to create arg if you need it:

 function debug(name, ...) local arg = table.pack(...) print(name) for i=1,arg.n do print(i, arg[i]) end end 
+11
source

This is because arg deprecated since Lua 5.1. It remained only as a function of compatibility.

References: Lua 5.1 Manual , unofficial LuaFaq

the workaround uses this line to create the arg table:

 local arg={...} 
+3
source

Source: https://habr.com/ru/post/1402419/


All Articles