lua-users home
lua-l archive

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

On 19 May 2010 22:42, David Manura <> wrote:
On Wed, May 19, 2010 at 6:51 AM, Nikolai Kondrashov <> wrote:
> I wasn't able to find an answer to a seemingly basic question anywhere:
> Is it possible to disable "assert" function execution along with its
> argument evaluation in Lua? I.e. like it is done in C by defining NDEBUG?

An observation: In many languages [1], asserts are clearly
distinguished from normal error handling: assert calls should have no
meaningful side-effects, so you may safely strip them, and any assert
failure indicates a defect in the program.  In Lua, assert is also
conventionally used for its convenience in converting normal error
return values into raised exceptions (the inverse of pcall).  The Lua
manual contains one such possible example: assert(loadstring(s))() .
This we might say is an abuse of the normally recognized concept of
assert and could call for splitting assert into two separate functions
(even if they be implemented identically).


David brings up a really important point.

Especially note that assert has return values, and so uses of assert in _expression_ contexts are one example of where simply removing them does not work. It seems to me that assert in lua plays a very different role than assert in languages such as c (where it can be removed with NDEBUG) and java (where it is turned off by default!). Maybe instead of removing assert, which has an established role in lua, a new construct specifically intended for side-effect-free checks should be used.