[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Safe navigation operator
- From: Wesley Smith <wesley.hoke@...>
- Date: Mon, 23 Aug 2010 15:24:55 +0200
> if config.foo.bar.baz then
> -- ...but this code will *always* be run, because if it *wasn't*
> -- already set, it will have been created dynamically by the metatable.
> end
I see. Well, this isn't possible without hacking Lua. You could
instead do this
local BAD_VAL = {}
local meta
meta = {
__index = function(t, k)
print("__index", t, k)
if(not rawget(t, k)) then
return BAD_VAL
end
return t[k]
end,
__newindex = function(t, k, v)
print("__newindex", t, k, v)
rawset(t, k, v)
end
}
setmetatable(BAD_VAL, meta)
-- config.network.server.url
local config = setmetatable({}, meta)
if(config.network.server.url == BAD_VAL) then
print("config.network.server.url BAD_VAL")
end