lua-users home
lua-l archive

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

On 1 January 2013 20:33, Thijs Schreijer <> wrote:
> The error function is defined as;
>     error (message [, level])
> The assert function is defined as;
>     assert (v [, message])
> Is there a specific reason the assert function doesn't have a 'level' argument that allows to throw the error up some levels? Eg. have assert defined as;
>     assert (v [, message [, level]])
> Currently I use;
>     if not v then error(message, level) end
> But I would prefer the shorter assert notation
> Thijs

assert is usually called along with a function:

    function foo()
        if some_cond then
            return data1, data2, data3
            return nil , "an error message"

    mydata1, mydata2. mydata3 = assert ( foo() )

This is very useful, as an error will be raised, or data is returned
for assignment.
Due to the behaviour of argument lists, it doesn't make sense for a
level argument:
the error message would be dropped in any case; and the multi-return would fail.