lua-users home
lua-l archive

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

>  if 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
		return t[k]
	__newindex = function(t, k, v)
		print("__newindex", t, k, v)
		rawset(t, k, v)

setmetatable(BAD_VAL, meta)

local config = setmetatable({}, meta)
if( == BAD_VAL) then
	print(" BAD_VAL")