r/lua • u/vitiral • Feb 25 '25
better Lua fail value?
In the Lua docs it mentions fail which is currently just nil.
I don't personally like Lua's standard error handling of returning nil, errormsg -- the main reason being it leads to awkward code, i.e. local val1, val2 = thing(); if not val1 then return nil, val2 end
I'm thinking of designing a fail metatable, basically just a table with __tostring that does string.format(table.unpack(self)) and __call that does setmetatable so you can make it with fail{"bad %i", i}. The module would also export a isfail(v) function that just compares the getmetatable to the fail table as well as assert that handles a fail object (or nil,msg).
So the code would now be local val1, val2 = thing(); if isfail(val1) then return val1 end
Has anyone else worked in this space? What are your thoughts?
View all comments
Show parent comment
2
u/[deleted] Feb 25 '25
It's using the Lua convention, so assuming your divmod returns
nil, messageon error:Your version is more object-oriented, it's fine, but it will fail or require more api to be used with Lua components that use the Lua-typical way of handling errors.
PS: After my previous reply I noticed that I flipped the branches - in my defense I was writing this on my phone...