3 ms·
The article doesn't get into it, but Rust has an error-handling operator, `?`, that encapsulates the branch-and-return-on-error check. Your code in Rust would l
by kibwen 6y ago
The article doesn't get into it, but Rust has an error-handling operator, `?`, that encapsulates the branch-and-return-on-error check. Your code in Rust would look like:
fn foo() -> SomeResult {
let result = bar()?;
let result2 = baz()?;
let result3 = bazz()?;
}
The fundamental difference between Rust's approach and Java-style checked exceptions arises when those three functions have different error types. In Java, you would annotate the function with every possible exception that the function can throw. In Rust, a library (or module within a library) would normally design its own Result type to encompass every error that the library/module can throw, and use that type on every error-producing function (you could reproduce the Java approach by defining a new Result type for each function, but I've never seen anyone bother with that). The end result is that you avoid the precise bookkeeping that so annoys people about Java's checked exceptions, but you also tend to lose some precision with reading the code: from reading the type signature you know that a function with a custom error type has the potential to error, which is useful, and you know that the error must be at least one of the types in the error's definition, but it might not exhibit all of the error types that the custom error type can contain. Think of it like a spectrum: from not having any indication that that a function may error (e.g. JavaScript), to knowing that a function may error but not having any indication of how (Swift), to knowing a function may error with at least one of a known set of types (Rust), to knowing that a function may error with an exactly known set of types (Java with checked exceptions).
- artificial 6y agoThank you for the excellent comparison with error precision. I’m diving into Swift (Vapor) and was curious if this remains true with Swift 5.2?
- kibwen 6y agoI'm no Swift expert, but I don't believe that any changes have been made to its idiomatic error-handling approach. Using the first example from the Swift 5.3 docs ( https://docs.swift.org/swift-book/LanguageGuide/ErrorHandling.html https://docs.swift.org/swift-book/LanguageGuide/ErrorHandlin... ): func buyFavoriteSnack(person: String, vendingMachine: VendingMachine) throws { let snackName = favoriteSnacks[person] ?? "Candy Bar" try vendingMachine.vend(itemNamed: snackName) } The `try` is analogous to Rust's `?`, and the `throws` is the part of the type signature that indicates that the function may error but without any additional information about how it may error. Of course, I imagine a capable IDE should still be able to inspect all the `try`s in a function to determine what types can possibly be thrown.
- NobodyNada 6y agoYes, that is still the case with 5.2. However, a proposal to add typed `throws` is currently being drafted and prototyped [0]. It won't be available in Swift 5.3, but if all goes well it might land in the next release after. (Also, if you're doing Vapor programming, you may be pleased to know that Swift 6 is expected [1] to bring a real concurrency model, which will likely include some form of async/await so that you won't have to muck about with all those nested closures.) [0]: https://forums.swift.org/t/typed-throws/39660 https://forums.swift.org/t/typed-throws/39660 [1]: https://forums.swift.org/t/on-the-road-to-swift-6/32862/ https://forums.swift.org/t/on-the-road-to-swift-6/32862/
- aboodman 6y agoWe define a custom error type for most functions in the Rust codebase I’m currently working on. It’s a little laborious but not that big a deal and I really enjoy being on the “completely precise” side of the spectrum :). It still feels lighter weight than Java’s checked exceptions and I think maybe that comes down to all the language machinery that Rust offers around patten matching and error combinators. I plan to blog about our error handling strategy... someday
- kibwen 6y agoInteresting. Although you're the first person I've heard about actually taking that approach, I have heard people lament that Rust library error types tend to be maximal rather than precise (IOW, peeved that a function's declared error type contains errors that that specific function can't actually produce; this is usually a jumping-off point to a spiel about asking Rust to support anonymous sum types), so I'm not completely surprised that somebody out there is actually doing it. That said, I've also heard people on the other side of the spectrum express that Swift's "I don't really care about the specific error type" is what they prefer; notably the anyhow crate (https://docs.rs/anyhow/1.0.32/anyhow/ https://docs.rs/anyhow/1.0.32/anyhow/) is a good way to get that behavior in Rust while remaining idiomatic.
- aboodman 6y agoI think it is possible to automate some of our approach with a simple macro, I just haven't gotten to it. I really like that you can look at a function and know every single type of exit it can have and know that you've handled them all. But this might be because the thing that we are building (replicache.dev) runs in someone else's process so we need to be good citizens and be very careful to never panic or return unexpected/undocumented errors.
- dllthomas 6y ago> the precise bookkeeping that so annoys people about Java's checked exceptions This just came up in a different context (https://news.ycombinator.com/item?id=24448865 https://news.ycombinator.com/item?id=24448865) but I don't think it's the precise bookkeeping that annoys people about Java exceptions so much as the fact that you often can't be all that precise and yet you still need to do a lot of bookkeeping.