lua-users home
lua-l archive

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


Hi,

John Belmonte wrote:
> 
> I'd like to suggest that assert add a linefeed to the message string for us.
> In the case of disabled traceback, lack of a linefeed leaves a "hanging"
> command line.  Try running this as a script:
> 
>   _ERRORMESSAGE = _ALERT
>   assert(nil, "oh boy")

IMHO your assumption that _ALERT is a suitable _ERRORMESSAGE function is
wrong.  All error msgs (including assert msgs) have no trailing '\n'.
Try issuing a normal error("foo") - it will be printed _wrong_, too.

The msgs passed to _ERRORMESSAGE are not lines but information strings.
And _ERRORMESSAGE's job is to build nice looking error messages from them.
By setting it to _ALERT your definition of _nice_ is: "just print it with-
out any reformatting" ;-)

If you want it nicer, i.e. make it a single line, use this:

  function _ERRORMESSAGE(msg)
    _ALERT(msg.."\n")
  end

Or if you only want a special behavior for assert (i.e. no traceback)
redefine it:

  function assert(cond, msg)
    if not cond then
      _ALERT("assertion failed! "..(msg or "").."\n")
      error() -- will not call _ERRORMESSAGE
    end
  end

Ciao, ET.