lua-users home
lua-l archive

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


On May 20, 2010, at 6:45 AM, Romulo wrote:

> In some recents projects I have implemented an interpolated version of
> assert, in which it justs comstructs the error message if the
> expression evaluates to false. Example:
> 
> mob = getmob()
> assert( mob:isalive(), "the mob {mob.name} must be alive" )
> 
> There is some black debug magic to get it working properly (handling
> locals, globals, function environments etc) and also the problem that
> it polutes the stack trace, but  I think it pays off in the end,
> specially because the only overhead you have is only activated when
> you need it, i.e. When the assertion fails.

I like this a lot. Certainly the annoying cost with assertions is frequently around constructing a meaningful error message that doesn't get used most of the time. The problem with just passing a function to evaluate to get the message is that we still pay the cost of constructing the function and its up values.

Does your approach work if debug symbols get stripped? I haven't looked to see whether that eliminates local variable names though I would expect it to do so.

Could one work around that with something like:

	assert( mob:isalive(), "the mob {%1.name} must be alive", mob )

Mark