lua-users home
lua-l archive

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


The assert function in Lua is very nice, but IMHO it has one major
drawback: string concatenation for its optional "message"-argument is
often necessary, but slow and happens too early. This is mildly
annoying for the Lua 5.2 implementation, and even worse for LuaJIT
(because the string concatenation prevents JIT compilation of the
surrounding code).

My suggestion is to not only allow strings as "message"-argument, but
also functions; if a function is supplied it only gets called when the
assertion actually fails and is expected to return a string describing
the problem. Example:

    local errfuncs = {}
    local error_str = function(code)
      errfuncs[code] = errfuncs[code] or function() return "Error " .. code end
      return errfuncs[code]
    end

    assert(something, error_str(errorcode))

This would make the assert- function significantly more useful IMO
(wherever performance matters); I find it desirable that programmers
use builtin functions when possible, because every user writing his
own "assert" variant adds unnecessary redundancy and confusion.

My apologies if this has been suggested before and shot down, or is
not viable for obvious reasons.

--Wolfgang Pupp