leevis.com
leevis.com copied to clipboard
lua table构造式的一个用途
lua中table是简直是万能的。 lua table构造式,可以实现有趣的用法,先来看个简单的例子:
local authors = {}
function Entry(e)
authors[e.author] = true
end
-- data began
Entry {
author = "foo"
}
Entry {
author = "bar"
}
-- data end
for n in pairs(authors) do
print(n)
end
输出
foo
bar
你把上述代码中data部分抽出放到一个文件中,用dofile替代,可以很灵活的实现一些功能。