-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: Go 2: ? :
for returning on function errors
#65579
Comments
IMHO it will be hard to come with a reasonable construct that handles and map the error that is clearer than However there are situations where you don't want to do anything with the error, but return it. Ex: if func foo() (*Data, error) {
data := getData()? // or getData()! or something else
doSomething(data)
return data, nil
} that will be the equivalent of func foo() (*Data, error) {
// num of vars `getData` returns + err
// | num of vars (zero value) `foo` returns + err
// | |
_v1, _err := getData(); if _err != nil { return nil, _err }; data := _v1
doSomething(data)
return data, nil
} Basically the new syntax would be used only when you write (I'd like a proposal with |
? :
for returning on function errors
I'm understanding this proposal as adding a new ternary operator that takes three operands:
The evaluation process would then be:
Please correct me if I misunderstood. 😀 The rest of what I wrote in this comment probably won't make sense if I didn't understand correctly. This does seem plausible in principle, but I think my main reservations are about the specific syntax you chose here:
A further random question, that doesn't really fit in the above list:
|
I still like the |
Based on the discussion above, and the emoji voting, this is a likely decline. Leaving open for four weeks for final comments. |
No further comments. |
Go Programming Experience
Experienced
Other Languages Experience
Rust, TypeScript, C, C++, C#
Related Idea
Has this idea, or one like it, been proposed before?
Yes
#65266
No pattern matching. Special error syntax for the error type.
Does this affect error handling?
Yes
Code focuses on the happy path. Code gets smaller. Error handling is still there.
We replace
with
Is this about generics?
No
Proposal
Add an operator that behaves similar to the
?
for Rust Results.If an expression contains an error that is not nil, it should wrap the error with a message and return it.
Or skip the wrapping and just return the error if no wrap message is specified.
Tools like gopls have to check if this is possible.
Language Spec Changes
No response
Informal Change
No response
Is this change backward compatible?
Yes
Now:
New
Some can still use the old syntax
Orthogonality: How does this change interact or overlap with existing features?
No response
Would this change make Go easier or harder to learn, and why?
Earier. Some people complain about the big parts of Go code being error handling. This could solve this while still keeping how error handling is done in Go.
Cost Description
Syntax change leads to changes in all tools that deal with Go Syntax. I don't think there is any runtime cost because it's basically syntactic sugar.
Changes to Go ToolChain
vet, gopls, gofmt
Performance Costs
No runtime cost. Can't tell about compile time but shouldn't be noticable.
Prototype
No response
The text was updated successfully, but these errors were encountered: