lua-users home
lua-l archive

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




2013/1/2 Daurnimator <quae@daurnimator.com>
On 1 January 2013 20:33, Thijs Schreijer <thijs@thijsschreijer.nl> 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
        else
            return nil , "an error message"
        end
    end

    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.

True, but there could be an assert function with a level that uses (level, assert_value, msg) as format. Would be useful to have such a function, I was similarly annoyed in the past about the lack of a level argument for asserts...

Cheers,
Eike