by unwrap_abuse · 3y ago · 0 comments
We wanted to get rid of C++ exceptions but traded it for panic hell.
Which brings me to the point: propagating errors is tedious enough that people are looking for shortcuts, which then cause other problems.
At a meta level, one of the reasons why there is so much focus on 'unwrap()' in particular is because it is often the precise point at which in your code where a runtime invariant is broken and thus leads to a panic. Nearly all code has runtime invariants in one form or another. The question is what happens when they're broken. In languages that are memory unsafe by default, the answer is often (but not always) "undefined behavior." In languages like Rust, or Python, or Go, the answer is often (but not always) "the process quits." The reality is more complicated than that, but those are a fine first approximation. For example, breaking a runtime invariant doesn't have to lead to undefined behavior or process termination. It can simply result in a logic error that leads to unexpected behavior.
Of course, making the issue more complicated is that sometimes 'unwrap()' is abused. And indeed, sometimes it is used in cases where an error ought to be returned. I find this to be generally pretty rare in popular libraries. But the key point here is that you can't just say, "oh I see unwrap() in a library, so now I'm going to scream ABUSE!!!!" It's more complicated than that.
Result is being sold on HN as a great solution to error handling while there’s several crates available which are needed to polish its rough edges, rough edges which have led to e.g. unwrap abuse in the past.
It is tedious to shuffle around results, but this is never part of the sales pitch. And for those of us that don’t want to depend on all sorts of crates for the simplest things, that’s the reality.
Re your reply “Maybe improve your reading comprehension. I said "(partial) nonsense." Therefore, some of what you said wasn't nonsense. But some is. What a fucking revelation.”
Partial nonsense isn’t nonsense by definition, I believe, but I won’t dwell that point. I am surprised seeing a Rustacean being (mildly) offensive and even saying the f-word. I must say that I am proud of you, too many nowadays act like polite, pedantic robots. :-)
I think you maybe meant converting between errors in Rust can be tedious sometimes, but propagation is pretty easy.
The conversation is something that generally need's to happen rarely (like a few times per project?), but `thiserror` and `from` make this pretty ergonomic.
> be careful: if you try to access an index which isn’t in the Vec, your software will panic!
> Use get() and get_mut() if you want to check whether the index is in the Vec.
In my experience, most people are using get() if the source of the index is untrusted
[0]: https://doc.rust-lang.org/std/vec/struct.Vec.html#indexing
Pro tip: most popular crates have excellent documentation (I know it's a shocker coming from other languages). So, check stuff before you use it.
I assume this is because the ecosystem lowers the barrier to entry for writing an generating documentation (compared to other ecosystems).
That's what it's supposed to do, RTFM.
Use .get() for safe access.
Sorry you were misled, but programming is, indeed, hard, and no, you can't go shopping instead.
Exceptions are a particular technique for dealing with errors in general, that can be used for both unrecoverable and recoverable errors. They infamously trade programmer convenience for hard to understand bugs, fragile run-time behavior and unmaintainable code.
Fragile run-time behavior and unmaintainable code is because programming is hard and cruft accumulates in the face of difficult requirements.
Renaming a thing that traumatized you in the past won't make the cause of the trauma go away.
Obviously this is not something you're encouraged to do often, but there's applications where it might be reasonable.