lua-users home
lua-l archive

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


2017-02-22 5:16 GMT+02:00 Martin <eden_martin_fuhrspam@gmx.de>:
> On 02/20/2017 03:36 PM, Soni L. wrote:
>>
>>     local function f(x)
>>       --assert(type(x) == "function")
>>       return {dothing=x}
>>     end
>>
>>     local v = f(nil)
>>
>>     v:dothing()
>>
>> If you
>> uncomment the assertion, you get an error on line "local v = f(nil)"
>> instead, which is much more helpful.
>
> I think official way to handle this is change assert() to error() which
> can specify in second parameter level of code that should take
> resposibility.
>
>   if (type(x) ~= "function") then
>     error('wrong arg type passed', 2)
>   end
>
> I've implemented similar functionality as family of assert_nil(),
> assert_boolean(), ... global functions.

I think I'm digressing here but ...

I use `assert` when the error is one of programming (i.e. it should
never happen, but unfortunately does) and `error` is one of the
user of the packages (i.e. bad input).