lua-users home
lua-l archive

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


David Given skrev:
[...] However, if the user makes a typo:
[...] I would like to be able to test for this.

[...] Any better suggestions?

Try this,

Lock down the access of non-defined values, this will give the user an appropriate (runtime) error message.

Here's a simple example:

local meta = {}
function meta:__index(key)
	local val = rawget(self, key)
	if val ~= nil then return val end
	-- uncommented in my test, since lua.c accesses undefined values
	-- if key ~= "foo" then return nil end
	error( string.format("`%s' is undefined", tostring(key)) )
end

setmetatable(_G, meta)

And tested:

> print("foo = ", foo)
stdin:1: `foo' is undefined
stack traceback:
        [C]: in function 'error'
        stdin:5: in function <stdin:1>
        stdin:1: in main chunk
        [C]: ?
>


Cheers,
Andreas