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

09-sequences-and-sparse-arrays/03-zero-based-bug/main.lua

lua · 0.5 KB

-- @runtimes lua54 luajit
--- 演示:把 0-based 索引搬进 Lua 会漏掉第一个/越界。
 
local M = {}
 
--- @return table { shifted_first = string|nil, correct_first = string|nil }
function M.run(log)
	log = log or print
	local names = { "Ada", "Bob", "Cara" }
	local shifted_first = names[0]
	local correct_first = names[1]
	log(("shifted=%s"):format(tostring(shifted_first)))
	log(("correct=%s"):format(tostring(correct_first)))
	return { shifted_first = shifted_first, correct_first = correct_first }
end
 
if not ... then
	M.run()
end
 
return M