3 ms·
> This is exception semantics which is hidden control flow present in every language with exceptions. This is not exception semantics. A language can make the
by ghoward 4y ago
> This is exception semantics which is hidden control flow present in every language with exceptions.
This is not exception semantics. A language can make the keyword "raise" do whatever it wants.
Should I have used some other keyword? Fine:
handle AllocationFailure;
In fact, let me expand it a bit:
restart
{
err = allocate(size);
if (err != NULL) handle AllocationFailure;
}
This code calls the AllocationFailure error handler when allocation fails, and has the option of "restarting" (i.e., going back to the restart keyword and trying again). There is no hidden control flow, unless you think `errdefer` is hidden control flow.
> It's not equivalent to calling a function because it destroys the stack context and violates the invariants and ability to reason about control flow for every function above it in the call stack.
No, it doesn't. Again, I implemented this in C. I don't use longjmp(). It does not destroy stack context. You seem to have no idea what you are talking about.
> You could guarantee that the stack will be reconstructed and execution will continue where it left off. But now 'raise' implicitly does a bunch of stuff including allocating memory on the heap and copying every stack frame between your error and your handler onto it. How does this function if your error is that you are unable to allocate?
Yes, by doing a function call. It's a function call. Really.
> Even then, you don't know where control will end up without hunting for the registration of the handler, which may not even be near the call site.
The registration of the handler does not need to use the stack. In my code, it uses a separate heap-allocated stack, which is in the thread-local data.
Did you not read the actual C code I sent you? It's all in there.
> Sure you can build a stack on the heap in userland in C and dump everything there so as to reconstruct it (I skimmed your code and it seems like some implementation of coroutines or similar, I'm notnsure if this is part of the error machinery). There's nothing in zig that prevents you from doing this, or even using that exact library.
It's not coroutines. It's simple function calls. It's using function pointers, yes, but it's just function calls.
> I either can't follow your code with the amount of effort I'm willing to expend (likely) or there isn't anything resembling the descriptions I can find for conditions and restarts there.
Because your mental model of conditions and restarts is completely wrong.
> Are you saying that the following can happen?
No, that is not what happens. This is what happens:
You have some function foo that is regular C code with the exception that it registers ipsom as a handler for some condition until foo returns.
So ipsom is a registered handler until foo is done executing.
Then foo can call bar, which calls baz, which calls qux, all of which are regular C code.
Then qux can call something which raises a condition of the kind foo registered.
Then control flow passes to ipsom, which gets a new stack frame, and which handles the error using the context in its new stack frame.
Then control returns back into qux with information provided by foo's handling code.
Then qux returns, followed by baz and bar returning without having to have any knowledge of or participation in your system.
And that all of this happens without constructing a closure over foo's context and sneaking it into qux via a global because foo's context is never used as the error handler!
Again, it's just function calls and a possible jump to the beginning of a marked block.
To show this, I have fully implemented restarts now ([1]), which I didn't do because I didn't need them yet.
Now, if you had a hypothetical foo that registers ipsom as an error handler, you would do this in foo:
y_REGISTER_HANDLER(ipsom);
// The rest of the code.
y_UNREGISTER_HANDLER;
I need to hook error handler registration into my RAII system so that you only have to register it (and it will automatically be unregistered on function exit), but you get the idea.
Now, in the function that actually raises the condition:
y_RESTART(restart)
{
err = function_that_returns_err();
if (err != y_ERROR_SUCCESS) y_HANDLE(err, status, restart);
}
Of course, it's C, so the macros are a little ugly and requiring passing in variable names, but it works.
In this code, if `err` is not a success value, then ipsom is called, which will either not handle the error, handle the error but not allow a restart, or handle the error and allow a restart. If the last one is what happens, the code will jump back up to the beginning of the y_RESTART block and start over.
Edit: I realized my commit forgot to add y_strucon_handleErrorWithRestart(). It was added in [2].
[1]: https://git.yzena.com/Yzena/Yc/commit/95312b059132dc3e8614b434ec336761befb9150 https://git.yzena.com/Yzena/Yc/commit/95312b059132dc3e8614b4...
[2]: https://git.yzena.com/Yzena/Yc/commit/61c4a3ef735446a9478c58c9ee7119125943446d https://git.yzena.com/Yzena/Yc/commit/61c4a3ef735446a9478c58...
- Schroedingersat 4y agoThen you haven't made conditions and restarts. You've made error values. This is just one trivial thing you can do with error values without adding keywords. As you said, it's just a function call. This is the error system that languages like zig and rust have. Ie. the best paradigm for error handling you can get without all thenoverheadnof a pure FP language. Except Zig has a bunch of erganomic enhancements to make them far more pleasant to deal with and reduce the mental overhead and scope for mistakes. You can't get the value out of a failable function without acknowledging and passing up or handling the error, and you don't need the verbiage of if err != nil adding noise everywhere. And it comes with batteries included so you're not going to use code that uses something stupid like errno instead without noticing. And you get errdefer to mitigate one of the biggest downsides to saying 'I can't handle this here, passing an error up'. They can't carry arbitrary data, because where would you put it in a language that has an error system capable of treating allocation failure as a handleable error? There's nothing stopping you from putting some state or context elsewhere, but you have to make those decisions. Similarly you can have the error get handled in a parent scope without unainding as I demonstrated if that's really really important, but it shouldn't be the default.
- ghoward 4y ago> You've made error values. This is just one trivial thing you can do with error values without adding keywords. As you said, it's just a function call. Well, yeah, with some "ergonomics." Like walking a stack of error handlers. Across threads. Go back and read my code; that's what it's doing: a stack walk across threads, followed by a jump backwards depending on certain conditions. Does Zig do that? Not builtin, for sure. Could you implement that in Zig? Sure, but then you're outside its "best-in-class" error system. > This is the error system that languages like zig and rust have. So you're admitting that Zig does not have any better error handling than C? Because C has return values. At least Rust lets you use Option values and Result values. > Ie. the best paradigm for error handling you can get without all thenoverheadnof a pure FP language. Sigh...I just implemented something from a functional programming language in C. Overhead is minimal. > Except Zig has a bunch of erganomic enhancements to make them far more pleasant to deal with and reduce the mental overhead and scope for mistakes. You can't get the value out of a failable function without acknowledging and passing up or handling the error, and you don't need the verbiage of if err != nil adding noise everywhere. That's just manual exceptions with the compiler complaining if you get it wrong. I mean, sure, it's great that the compiler will check your work, but Clang and GCC can give me warnings about unused return values, so...that's not much better than C. And they are still exceptions. You're still destroying stack frames until some function higher in the stack handles the error. When it does, all of that context is gone. Using conditions and restarts means that you can make the decision to tear down the context after* handling the error. Exceptions are much less worse in that case. I've actually found them to be useful in certain cases, but only if the error is handled first. Also, you can add those ergonomics in C. Like so: #define try(var, expr) do { typeof(var) ret_ = (expr); if (is_err(ret_)) return ret_; var = ret_; } while (0) > And it comes with batteries included so you're not going to use code that uses something stupid like errno instead without noticing. And you get errdefer to mitigate one of the biggest downsides to saying 'I can't handle this here, passing an error up'. I will say that errdefer is a good idea. I did implement basically that. But no, Zig's error system doesn't come with batteries included because it can't carry arbitrary data. And it doesn't do a walk of a stack of registered handlers. And it doesn't do restarts. And it doesn't have Option or Result, nor interfaces so you could implement the same thing for other types. Whatever you might think of conditions and restarts, if you seriously think that Zig's error system is best-in-class when Rust's does the same thing and more, while also allowing user code and types to use the same infrastructure, I just don't know what to tell you. And I'm no fan of Rust. But it's better than Zig. > They can't carry arbitrary data, because where would you put it in a language that has an error system capable of treating allocation failure as a handleable error? Preallocate on thread startup. This is not hard. And on allocation error, every function that allocates is controlled by the language (through the standard library), so that's not a problem in practice. And even if something tries to wrap an allocation failure in more data, and allocation fails again, just panic(). Really. You can even document that behavior in the language. > There's nothing stopping you from putting some state or context elsewhere, but you have to make those decisions. Yes, but that's an admission that Zig's error system is not ergonomic. Yet you claim that it is best-in-class simply because it's slightly more ergonomic than C's? Sigh...