lua-users home
lua-l archive

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


> I call it 'pseudo' because it would be part of the grammar - only way
> one could get that late evaluation.

Or you could use loadstring:


format = string.format

function lazy_assert(cond, errmsg)
	if(cond) then
		return cond
	else
		local env = setmetatable({}, {__index = _G})
		return nil, setfenv(loadstring(errmsg), env)()
	end
end

x = 1
print(lazy_assert(x == 1, [[return format("bad equation %f == 1", x)]]))
print(lazy_assert(x == 2, [[return format("bad equation %f == 2", x)]]))



Of course, you can't use locals here, but if you could pass in an env
table or varargs somehow to lazy_assert it would be possible.

wes