4 ms·
Why the handler function over something like Rust's propagation operator? [0] I see that it adds significant flexibility, but at the cost of verbosity and a ne
by reificator 8y ago
Why the handler function over something like Rust's propagation operator? [0]
I see that it adds significant flexibility, but at the cost of verbosity and a new keyword that largely conflicts with one of Go's niches right now, web servers. And that flexibility seems likely to go unused in my experience. I would be shocked to see anything other than `return err` inside that block.
Sure errors are values and all that, and maybe I'm just working on the wrong codebases or following worst practices. But generally I see three approaches to errors in Go code, in order of frequency:
1. Blindly propagate down the stack as seen here. "It's not my problem- er, I mean, calling code will have a better idea of what to do here!"
2. Handle a specific type of error and silently recover, which does not typically need a construct like this in the first place.
3. Silently swallow errors, driving future maintainers nuts.
This seems to only really help #1, but `return thingThatCanError()?.OtherThing()?` can handle that just as well.
[0]: https://doc.rust-lang.org/book/second-edition/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator https://doc.rust-lang.org/book/second-edition/ch09-02-recove...
- DannyBee 8y agoI believe this is explicitly discussed in the doc: https://go.googlesource.com/proposal/+/master/design/go2draft-error-handling-overview.md https://go.googlesource.com/proposal/+/master/design/go2draf...
- bvinc 8y agoTheir discussion of Rust's error handling is woefully incomplete. They seem to think that the only thing that exists is the ? operator, which will return your error early, or a match statement, which is verbose. It misses some important aspects of rust's error handling. 1. There is a typed conversion between one type of error and another, which can keep context. 2. There are traits which can extend error types. 3. Backtraces can optionally be kept on (at a performance hit during errors of course). I have a lot of code written that looks like this, using the excellent "failure" crate, which allows optionally to add context to any error: file.seek(SeekFrom::Start(offset)) .context(format!("failed to seek to {} in file", offset))?; You get explicit errors, minimal code, optional error context. It's pretty perfect.
- DannyBee 8y ago"Their discussion of Rust's error handling is woefully incomplete." These are clearly not intended to be dissertations on the topic. However, i don't think anything you said changes their discussion, actually.
- stevenhuang 8y ago> However, i don't think anything you said changes their discussion, actually. How does it not? It seems to me that OP revealed an alternative method of error handling (which is very cool I might add), exactly in response to Google's discussion on Rust's error handling. If the discussion doesn't address this, then it seems reasonable to say it's incomplete, as its omission makes it appear they're not debating Rust at its strongest. edit: paper->discussion
- DannyBee 8y agoBecause none of the things said actually refute any of the points in the Google discussion? At all? I also definitely don't feel a failure to go off and survey every random library that exists for rust makes a discussion "woefully incomplete" or that it isn't debating rust at it's strongest. The same treatment is given to all languages, including rust, go, etc. The discussion is also about language features, and it discusses the language features.
- reificator 8y agoThat snippet looks like it'd be very helpful, I'm going to make a note to check out that library. Thanks!
- gnuvince 8y agoI got turned off of failure when a reddit user reported that it slowed down his program by 300x and the author of failure's response was that the original poster had misused failure. https://www.reddit.com/r/rust/comments/7te8si/personal_experience_can_cause_hard_to_notice/ https://www.reddit.com/r/rust/comments/7te8si/personal_exper...