lua-users home
lua-l archive

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


* Rebel Neurofog:

> assert () is the best in your case (more verbose)
>
> error () may also be helpful
> especially if your error message requires complex evaluation:
> -- This requires 'some_complex_call ()' even if there was no error:
> assert (bar ~= "", "Error: "..some_complex_call ())
>
> -- While this will call 'some_complex_call ()' only in case of error
> if bar == "" then
>    error ("Error: "..some_complex_call ())
> end

You can define something like this:

function fassert(condition, f, ...)
   if not condition then
      error("Error: " .. f(...))
   end
end

(The "Error: " prefix is actually unnecessary, I think.)

Then you can write:

  fassert(bar ~= "", some_complex_call)

Or if some_complex_call takes any arguments:

  fassert(bar ~= "", some_complex_call, arg1, arg2)

Perhaps this is good enough for your purpose?