lua-users home
lua-l archive

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


* Marc OMorain:

> There are very often hidden penalties associated with assert.
>
> Consider the following code, where object is a userdata (userdatum?)
> (This would be a very common code pattern in my field).
>
> assert(object:isValid(), "object " .. object:getName() " .. " is not valid")

You could turn that into

  assertmethodcall(object, "isValid")

with something like this in the debug version:

function assertmethodcall(object, method, ...)
    local f = object[method] -- needs test for f being nil
    local state, message = f(object, ...)
    if not state then
        -- do a debug dump of object, method, and ...
        error("assertion failed")
    end
end

Basically, the idea is to move the actual call into the assert, so
that evaluation can be made conditional.  Hopefully, if your debug
dump includes all the arguments, you don't need to construct an
explanatory string manually.