[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: An interesting new ways to do errors
- From: HyperHacker <hyperhacker@...>
- Date: Mon, 21 Nov 2011 22:40:02 -0700
On Mon, Nov 21, 2011 at 21:42, Daurnimator <email@example.com> wrote:
> That errors should be resumable:
> - for warnings: sometimes you just want to ignore them and continue
> - some errors can be recovered with user input
> So I had a grand idea: re-implement errors as we know them in lua;
> with coroutines!
> - error raising functions (such as error and assert) are replaced
> with coroutine.yield
> - pcall just becomes a wrapper around coroutine.create
> I went and made a small library to try it; and it works great!
> A few problems occured along the way:
> - varargs + variable length return values are a bitch: so many
> functions! everything has to be a tailcall too; or you blow the stack.
> - xpcall becomes slightly incompatible with existing handlers: you
> are no longer running in the error'd thread in the error handler: the
> thread that had the error is passed in (and you can use this parameter
> to get information with debug.getinfo()/debug.traceback()/etc
> A few realisations came about:
> - This wasn't only a system for adding resumable errors; it is a good
> way to raise and catch errors overall
> - You get yielding over pcall boundarys for free
> - The standard lua error system is no longer needed: adding something
> like this to the standard distribution may really simplify thing: “A
> designer knows that he has achieved perfection not when there is
> nothing left to add, but when there is nothing left to take away”
> What do you think?
> Would you use something like this in your own code?
> Should the error system as we know it be replaced?
Funny, I had a very similar idea the other day:
local errhandler = coroutine.create(function(what)
while true do
io.stderr:write("Error: " .. what .. '\n')
what = coroutine.yield(what ~= "It's broken")
local ok, stat = coroutine.resume(errhandler, what)
if not ok then error("Error in error handler: " .. tostring(stat)) end
if not stat then error("Unrecoverable error: " .. tostring(what), 2) end
print("this should never be reached")
print("this should also never be reached")
If it were possible to kill coroutines, the error handler could even
do that for severe errors. (In your own applications, you could just
have a table of coroutines that run one after another, and just remove
it from there...)
Related: I'd love a standardized warning system in Lua. It could be as
local here = debug.getinfo(2, 'Sl')
print(("Warning: %s:%d:"):format(here.short_src, here.currentline), ...)
and then you just replace it with your own function if you want to
handle warnings differently.
Sent from my toaster.