lua-users home
lua-l archive

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


Am 18.01.2014 12:08 schröbte Dirk Laurie:
2014/1/18 Justin Cormack <justin@specialbusservice.com>:
On Sat, Jan 18, 2014 at 9:59 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
2014/1/18 Peng Zhicheng <pengzhicheng1986@gmail.com>:

IMHO, `assert' and `error' have quite different semantics and use cases.

So, it is quite reasonable for me that `assert' only accept a string
message, since it is to give the (interactive session) user some clue
about what went wrong, instead of to give `pcall' the `error object' to
examine (and maybe to retry or recover) in a programamtic way,
in which case `error' should be used.


I think you are missing Peng Zhicheng's point, which can be
paraphrased as:

Programs have two kinds of errors:
    1. Bugs. When detected, use assert() with its blunt and brutal stop.
    2. User errors. When detected, use error() with all its flexibility.

That's the way asserts are used in C. It is not necessarily how `assert` should be used in Lua. I think we already had this discussion a year ago[1].


I.e. the following code is bad:

n=io.read()
assert(tonumber(n),"you did not enter a valid number")

You should have been using error().

The following code is good:

assert(heap,"Program error: heap has not been initialized.")


... for a suitable definition of "good" and "bad".


Philipp

  [1]: http://lua-users.org/lists/lua-l/2013-01/msg00084.html