[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Proposal: Add "level" argument to assert()
- From: HyperHacker <hyperhacker@...>
- Date: Sat, 9 Apr 2011 08:41:29 -0600
assert() is a convenient function for checking for error conditions in
one statement. A common expression:
if not foo then error("Invalid foo") end
can be shortened to:
assert(foo, "Invalid foo")
One common use of such an expression is to validate function arguments, e.g.:
function loadFile(path)
assert(type(path) == 'string', "Invalid path given to loadFile()")
--...
end
This use is common enough that the C API provides a few macros for it,
such as luaL_argcheck and luaL_typerror. However the above example has
a small problem: it reports the error as being caused by the call to
assert() itself, when the actual problem is at the point loadFile()
was called with an invalid path.
To solve this problem, error() has a "level" argument, that lets you
specify the position on the call stack of the actual error. So, why
not add such an argument to assert() as well?
--
Sent from my toaster.