5 ms·
Yes, it's amazing that a language made when Go was is basically a version 1.5 of languages made 40 years ago. C had the worst possible error handling and Go ha
by alien_at_work 9y ago
Yes, it's amazing that a language made when Go was is basically a version 1.5 of languages made 40 years ago. C had the worst possible error handling and Go has swooped in to take the second spot.
The very best error handling I know of is in CLOS. Instead of exceptions, they have "conditions". A condition is signaled like an exception but with a big difference described below. The thing about errors is, the code that detects the error can never know how to deal with that error. If it did, then it wouldn't be an error it would be a case within that function. However, that code may know ways the error could be dealt with, it's just not in a position to choose. So what conditions do is, you can define a condition and so called "restarts" along with that condition. What the restart does is say "here is a way I could handle this error" and code higher up the stack has the option to (1) use the restart, (2) define additional restarts on that condition or let the condition propogate upwards. The condition signalling does not necassarily unwind the stack: if you choose a restart, the code continues in the stack that restart was defined.
So this means every part in the stack that knows how a condition could be recovered from can provide the option but it is left to code higher in the stack to chose the actual strategy. This is superior to the exception strategy of unwinding the stack until you find a handler because sometimes you really want the code to continue execution where it is.
But, of course, such a system is really expensive to implement. If you can't have that my preference is the Haskell style of using a sum type where errors can happen but writing as much of your code as possible with any error conditions prohibited by the type system.
- steveklabnik 9y agoFun fact, Rust had conditions a very long time ago https://doc.rust-lang.org/0.9/guide-conditions.html https://doc.rust-lang.org/0.9/guide-conditions.html Nobody used them, so we removed them.
- flavio81 9y agoWell, that's really bad, because I think that things should be implemented in the best way possible. "Nobody used them", yeah, probably because no other language (apart from CL) had it, so nobody knew how to leverage them. But leave it in place and at some point in future time, some blogger would write a nice article on using them in Rust and developers would catch up in this cool feature. I develop in Common Lisp and conditions&restarts are so much superior to the other error handling paradigms i've seen, that it's not fun being left without them.
- steveklabnik 9y agoAt the time, those docs were the error handling documentation, so people did know they existed. They still weren't used anyway.
- alien_at_work 9y agoI think the point is that sometimes showing documentation that something exists isn't enough for everyone to "get it". Sometimes you need some kind of technology evangelist to show something really cool they did with a feature for the creative process to begin on a larger scale. Out of curiousity, if this was just a macro and no one was using it, what would have been the harm in just leaving them in? Is this a part of the language that changes so much it made maintainance a pain?
- steveklabnik 9y agoMacros in Rust are global, insta-stable, and have no namespacing. This means any macro we put in std means you cannot define a macro in any Rust program with that same name. This is one of the reasons we've been working on a new macro system, to fix these kinds of issues. "Just leave it in" isn't a good justification for a feature. We'd then require that every implementation of Rust ever, forever, implements this too. Being in std is a high bar. Beyond that, there are a host of other reasons: https://github.com/rust-lang/rust/commit/454882dcb7fdb03867d695a88335e2d2c8f7561a https://github.com/rust-lang/rust/commit/454882dcb7fdb03867d...
- draegtun 9y agoPerl6 & Smalltalk also have them (conditions&restarts). And i've seen modules to add condition systems to perl5, Ruby & Clojure. But these are kinda of hacks so the more languages that have it baked-in the better!
- flavio81 9y agoThanks, I stand corrected. It seems Perl has everything!