Re:Lies we tell ourselves to keep using Golang
by packetlost on Tuesday November 26, 2024 @05:04PM
(#42250562)
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.
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)`