▲ | miki123211 a day ago | |
> I agree that Go should really have an analogue to Rust's `?`, but you can't really do that in a sane way without sum types to represent your conditions. The very multiple-return style error propagation makes it impractical to do. There was a proposal for a `try`, which I still think should have been adopted. Under that proposal, `someComplicatedExpression(try(functionReturningError()))` would be converted to `foo, err := functionReturningError(); if err != nil{return zeroValue, zeroValue, err}; someComplicatedExpression(foo)` | ||
▲ | packetlost a day ago | parent [-] | |
You would need some form of compile-time reflection/specialization to implement that properly (what if the second return value isn't an error? What if there's only 1 return value?). Further, you would lose the ability to add context to the error branch via fmt.Errorf, which seems rather critical to understandable error conditions. I'm not sure I would be satisfied with any implementation of try as the language is now, I assume the Go language team would probably feel the same. |