示例工程 09-sequences-and-sparse-arrays

09-sequences-and-sparse-arrays/04-pack-n/main.lua

lua · 0.4 KB

-- @runtimes lua54 luajit
--- 演示:table.pack 记录含 nil 的参数数量。
 
local M = {}
 
--- @return table { n = integer, first = any, last = any }
function M.run(log)
	log = log or print
	local packed = table.pack("a", nil, "c")
	log(("n=%d"):format(packed.n))
	log(("first=%s last=%s"):format(tostring(packed[1]), tostring(packed[3])))
	return { n = packed.n, first = packed[1], last = packed[3] }
end
 
if not ... then
	M.run()
end
 
return M