示例工程 05-variables-and-scope

05-variables-and-scope/02-local-x-equals-x/main.lua

lua · 0.4 KB

-- @runtimes lua54 luajit
--- 演示:local x = x 右侧读到的是外层 x。
 
local M = {}
 
--- @return table { before = integer, after = integer|nil, shadowed = boolean }
function M.run(log)
	log = log or print
	local x = 10
	local x = x + 1
	log(("after=%d"):format(x))
	local fresh = fresh
	log(("fresh_nil=%s"):format(tostring(fresh == nil)))
	return { before = 10, after = x, fresh_nil = fresh == nil }
end
 
if not ... then
	M.run()
end
 
return M