4 ms·
I haven’t used Rust. Does the ? operator add some kind of stack trace to the error? If not, how do you know where the error has come from? Having a Result type
by jamespwilliams 5y ago
I haven’t used Rust. Does the ? operator add some kind of stack trace to the error? If not, how do you know where the error has come from?
Having a Result type with statically enforced checking sounds good though.
- gilbetron 5y agoDoing foo()? is equivalent to: err := foo() if err != nil { return err }
- masklinn 5y ago> I haven’t used Rust. Does the ? operator add some kind of stack trace to the error? No. `?` performs an optional conversion and an early return, that’s it (kinda). > If not, how do you know where the error has come from? By default you don’t. There are utility libraries you can use to attach such information to error types (and eventually the stdlib will provide one), but OOTB an error value is… quite literally anything (it should probably implement `Error` but doesn’t actually have to). This is in keeping with the idea that hidden costs should be avoided when possible, and that reified results should be the norm so should have as little requirement as possible.
- kevinmgranger 5y ago> Does the ? operator add some kind of stack trace It depends. `?` will convert the inner error type (the value from the function to its left) to the outer error type (the return type of the function containing this line). That conversion function can be defined by the inner error type. If the conversion generates a stack trace (or adds to an existing one), then there will be one. Therefore, "Is there always a stack trace available?" is a tricky question-- it was seen as unimportant during the 1.0 days because they wanted to be able to accommodate low-resource environments. Today, there's third party error handling libraries that cut out error creation / manipulation boilerplate, that might create stack traces for you, and there's efforts ongoing to see how it might be added to the stdlib.