You can fix that easily with a metatable.
function nilguard(tbl)
tbl = tbl or {}
local mt = {
__index = function(t,k)
error("Invalid key: " .. tostring(tbl) .. "[" .. k .. "]")
end
}
setmetatable(tbl, mt)
return tbl
end
local myObj = nilguard({foo=10})
myObj.crap -- should raise
For undefined global variables, just require("strict")
Re:Why Lua?
by silentbicycle on Tuesday January 31, 2012 @07:20PM
(#3535966)
You can set __index on the global table's metatable. Basically: "run this hook whenever I try to access an undefined field".
setmetatable(_G, {__index=function(global_env, name) print("unknown: " .. name) end})
Other than that, I agree with the other posters here. It's an impressively lightweight and elegant language. It's especially good for embedding: its C integration is next to none, it's easy to sandbox if you want to run untrusted code, and the interpreter doesn't use any global variables.