lua-users home
lua-l archive

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


If I have a function

function f() return nil, setmetatable({}, {__tostring = function()
return "error" end}) end

and I use assert on it I annoyingly get
assert(f())
stdin:1: bad argument #2 to 'assert' (string expected, got table)

Since 5.2, error() has been fixed to call __tostring it seems, so you
do not get "error object is not a string" messages.

I find non string error types to be incredibly useful, you can give
them predicates so the user can find out more about the error in the
code, but this means casual use of assert is hard. I end up redefining
assert always, just to add the tostring call.

Is there any reason for assert not to always call tostring?

Justin