示例工程 02-values-and-types

02-values-and-types/04-reference-equality/main.lua

lua · 0.7 KB

-- @runtimes lua54 luajit
--- 演示:表/函数按引用比较,字符串按值比较。
 
local M = {}
 
--- @return table { table_eq = boolean, alias_eq = boolean, func_eq = boolean, string_eq = boolean }
function M.run(log)
	log = log or print
	local a = { value = 1 }
	local b = { value = 1 }
	local alias = a
	local f = function() end
	local g = f
	local s1 = "abc"
	local s2 = "ab" .. "c"
	log(("table_eq=%s"):format(tostring(a == b)))
	log(("alias_eq=%s"):format(tostring(a == alias)))
	log(("func_eq=%s"):format(tostring(f == g)))
	log(("string_eq=%s"):format(tostring(s1 == s2)))
	return {
		table_eq = a == b,
		alias_eq = a == alias,
		func_eq = f == g,
		string_eq = s1 == s2,
	}
end
 
if not ... then
	M.run()
end
 
return M