lua-users home
lua-l archive

Re: level argument for assert

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


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.

AltStyle によって変換されたページ (->オリジナル) /