5 ms·
I tend to check the error handling capabilities of a language to see how expressive and easy it is to return errors. I personally prefer result types over optio
by larusso 3y ago
I tend to check the error handling capabilities of a language to see how expressive and easy it is to return errors. I personally prefer result types over optionales (e.g. the mentioned added context instead of a nothing). It seems that zigs shoots in the middle here. I like the implicit errors shown but also feel that the lack of context isn’t great. What is missing for me is chaining and a simple message. That being said I‘m also not super happy how rust manages errors. At the moment I write my tools and crates with „thiserror“ and „anyerror“ which also illustrates how errors most of the times are uses. I say most since it really depends. In libraries I tend to introduce an error type per bigger module. And wrap low level errors so it results in a chain of errors (similar how Java does it with their exceptions) This gives me the most context and a kind of breadcrumb. I hate when you execute something and a naked IO error ala „Permission Error“ is returned. But this all means a lot of extra code that starts to hide the simple implementation under the hood. For simple apps where any error means „Sorry I‘m out of luck“ I use anyerror to not write custom FromError implementations. Again this all illustrates a bit that I‘m not super happy in rust. But I actually never encountered an error system which can keep it simple and fast. The Zig examples are nearly there though! Just the missing error context …