5 ms·
There's much I don't love about Rust, but I feel golang could steal the ? operator and keep the spirit of go. Effectively, instead of result, err := doSo
by fizx 3y ago
There's much I don't love about Rust, but I feel golang could steal the ? operator and keep the spirit of go.
Effectively,
instead of
result, err := doSomething()
if err != nil {
return nil, err
}
you'd get the same control flow with
result := doSomething()?
- tialaramex 3y agoTry (the current incarnation of the ? operator) is actually a very clever trait which does rather more than that. Types for which Try is implemented can Try::branch() to get a ControlFlow, a sum type representing the answer to the question "Stop now or keep going?". In the use you're thinking of where we're using ? on a Result, if we're Err we should stop now, returning the error, whereas if we're OK we should keep going. And that's why this works in Rust (today), when you write doSomething()? the Try::branch() is executed for your Result and resolves into a Break or a Continue which is used to decide to return immediately with an error or continue. But this is also exactly the right shape for other types in situations where failure has the opposite expectation, and we should keep going if we failed, hoping to succeed later, but stop early if we have a good answer now.
- vacuity 3y agoA big problem with Try is the function signatures...excuse me, I would like a <<T as Try>::Residual as FromResidual<Result<T, !>>::Output, please. Yes, that is a caricature and I don't know the proper signatures, but c'mon. Read the discussion for the Try v2 RFC if you want a better idea. ...and then they add more syntax sugar to partly sweep the complexity under the rug. I like Rust as much as the next person, but I'm apprehensive about how this will play out.
- cedws 3y agoThat won't work because in Go you often need to wrap errors with additional context. I have worked with Rust Option/Rust types and found them extremely unergonomic and painful. The ?s and method chains are an eyesore. Surely PLT has something better for us.
- vips7L 3y agoDo go errors or rust options include stack traces?
- deleted 3y ago[deleted]
- steveklabnik 3y agoIn Rust you can wrap a context by chaining a context method on before the ?, at least with libraries like anyhow.
- bruh2 3y agoWhat does PLT mean in this context? I wasn't able to $searchengine it
- cedws 3y agoProgramming Language Theory