5 ms·
I find it surprising that so many people are arguing about the benefits and drawbacks of `?`, when in my experience the handling of Result and Option haven't be
by estebank 4y ago
I find it surprising that so many people are arguing about the benefits and drawbacks of `?`, when in my experience the handling of Result and Option haven't been an issue in practice on the consuming side (`?`, `.unwrap()`, `.map()`, `.ok()`, if let, match, let chains, let else, etc. help a lot), but where all the pain comes from is having to declare the appropriate error type itself. Libraries like `anyhow` takes some of the pain away, but declaring an appropriate struct or particularly an enum in the right places, and the boilerplate for all the type conversions (From/Into impls) are where, during development, I have frustration. What I do then is either use Result<T, ()> or a single `struct Error(String);`, and go back once I have all the scaffolding in place and pry the implicit error tree back into the type system. Anonymous enums like typescript (`A | B | C`) could presumably help here.
- lenkite 4y agoAll these workarounds for easier Result handling truly make me wonder whether Rust will eventually evolve exceptions as a feature - of-course without explicitly terming them so.
- steveklabnik 4y agoThe community would revolt. Not going to happen. Proposals to make the existing syntax and semantics even look more like exceptions were met with lots of hostility.
- FpUser 4y agoRevolt how? Go back to C++ or whatever language/s they were using?
- steveklabnik 4y agoI don't know exactly, but it's never pretty when a project's leadership makes an unpopular decision. Lots of complaining, for sure.
- tomp 4y agoRust already has exceptions, they just call them panic and offer exceedingly poor language support for them. Some people simply live in denial, believing untruths that confirm their worldview. This is the same situation as with Go and generics. The maintainers were in denial for a very long time, before finally admitting what was obvious to some of us long before [1] [1] https://news.ycombinator.com/item?id=8626978 https://news.ycombinator.com/item?id=8626978
- xcdzvyn 4y agoThe implementation, outcome, use-case, and characteristics of panics are all very different from exceptions. The only marked similarity between the two is that they both interrupt program execution (and in different ways). But I feel as if you already knew that.
- tomp 4y agoPanics support stack traces, unwind the stack, releases the memory, and can either be recovered in some situations, or prints an error message. Sounds like Python exceptions.
- a5huynh 4y agoI've run into similar issues and found that the `thiserror` crate (https://crates.io/crates/thiserror https://crates.io/crates/thiserror) combined w/ anyhow makes a lot of that pain go away
- steveklabnik 4y agoFor those reading this that aren't super familiar, common Rust advice is "use thiserror for libraries and anyhow for applications," as they make slightly different tradeoffs and so are useful, especially together.
- chrisgacsal 4y agoI would add `snafu`(https://crates.io/crates/snafu https://crates.io/crates/snafu) here as a good alternative to thiserror+anyhow.
- echelon 4y ago`anyhow` + `?` make writing an application as smooth as butter. You won't miss exceptions. Don't use `anyhow` for libraries, though. You want to provide your consumers the ability to `match`.
- anacrolix 4y agohere we go again!