lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>  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