-
Notifications
You must be signed in to change notification settings - Fork 397
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
feat: another improved error handling proposal #1018
Comments
Assuming that you propose a variation of golang/go#32437, f := try openFile() replaced by: f, err := openFile()
if err != nil {
return err // or return ..., err where ... are func output parameters
} with Could it be used to handle chained calls as well: try a().b().c() where all |
Robert's proposal and mine are kind of based on Zig's error handling. Yes, |
Applying It was not considered before because "chained calls are not so common in Go" (in the FAQ discussion of Griesemer proposal), but having error processing here would be an enabler for this pattern, very common in data processing environments. To me it is a good synthesis between functional language philosophy, emphasising function composition, and system programming, where solid error processing is often mandatory at all steps. In this context, there is a multiplying effect on the conciseness, worth to be considered: Going from: r1, err := a()
if err != nil {
return err
}
r2, err := r1.b()
if err != nil {
return err
}
r3, err := r2.c()
if err != nil {
return err
} to: r3 := try a().b().c() |
Do we agree that the target of a failing try is to panic, not to return anything, right? |
It's not and never has been about I understand the concern, where in other languages the pattern To me, the confusion quickly disappears because the form would be entirely different between Go and Java. In Go, The keyword |
If we want to panic instead of returning an error using func f() (err error) {
r := try g() // f() will return an error if try fails
defer func() {
if err != nil {
panic(err)
}
}()
r := try g() // f() will now panic if try fails
} |
My 2c; I think that any syntax-level language proposal, which has already been proposed & rejected in Go, needs to satisfactorily answer the following question: why does the feature make sense in Gno, going against the decision in Go? The reason why I think this is worth asking for all similar proposals is not that I think the Go team is always right, but I think that any syntax-level change we add that deviates from Go creates learning curve for a developer jumping from one language to the other. The reason for the change must be solid and heavily justified, especially when it adds whole new constructs like this one. For this reason, while I'm not necessarily outright against all language changes that deviate from Go, I do think a strong argument needs to be made relating to "why Gno is different". I think in this case, the burden of proof would lie on @peter7891, and I can see this as some potential for sense in Gno (as compared to, say, gnolang/hackerspace#109 -- where the only good talking point for the question I could think of was for global error variables). To me, the reason this could make sense discussing is that in Gno, |
To be fair, the proposal that was rejected in Go was not on the syntax-level but a function call. |
My perception is that there may be a chance for Petar's proposal to Go: The original golang/go#32437 was about a builtin, not a keyword as proposed in Petar's one. I'm sure that adding
I may be wrong on the last one, and it needs to be looked at carefully. Another proposal golang/go#60720 also introduces You may think this ship has sailed, but better error handling is still the #1 issue today in Go (and therefore in Gno too), and we are at the perfect place to experiment a bit more and go further than just exercise thought. |
I noticed that in addition to distributing over chained calls, i.e. applying d := try a(x).b(y).c(z)
d := try a(x)(y)(z) Also, by being idempotent for functions where the last output parameter is not an error (so in that case, function call results are returned by In the case of nested calls, d := try c(try b(try a(x))) Note that the absence of additional parenthesis (as opposed to Consider that the above single line examples replace original code blocks of 12 lines each (depending on the number of calls), and allow new functional programming patterns while retaining the error handling already present in Go. All of this was absent from the original proposal, and others that I've seen so far. To me it makes the Peter proposal even more compelling and worth being submitted to the Golang team. |
I propose introducing a new keyword
try
to tackle the problem ofif err !=nil
spam.Curretnly we have
This can quickly get out of hand.
I propose the code to look something like
The way it will work is,
try
will be replaced by the normal error handling we have now, during preprocessing.Many details are to be defined. This is just a conversation starter.
The text was updated successfully, but these errors were encountered: