go: proposal: Go 2: error handling: try statement with handler
Author background
- Experience: 4 years experience writing production Go code. Expert at writing
if err != nil. I have forked and created Go error handling libraries. - Other language experience: Many of the common languages that are not JVM/CLR (Rust, Haskell, Python, Ruby, TypeScript, Nim, SQL, bash, terraform, Cue, etc)
Related proposals
- Has this been proposed before? Variations have been proposed, this is discussed in the proposal.
- Error Handling: Yes
Proposal
Add a new statement try that allows for succinct error handling.
try err, handler
This is translated to
if err != nil {
return handler(err)
}
Zero values are generated for any return types, so to see this in the context of a function:
func(args...) (rtype1, rtypes..., rtypeN, error) {
try err, handler
...
}
turns into the following (in-lined) code:
func(args...) (rtype1, rtypes..., rtypeN, error) {
if err != nil {
return Zero(rtype1), Zeros(rtypes...)..., Zero(rtypeN), handler(err)
}
...
}
The handler argument is optional
try err
This is translated to
if err != nil {
return err
}
Unlike in previous proposals, try is a statement, not an expression.
It does not return any values.
When a function returns non-error results, an intermediate error variable must be used.
x, err := f()
try err
If an expression returns just an error, it is possible to use try directly on the expression without an intermediate variable.
For the sake of consistency it may be desireable to not allow this form (possibly enforced by linter rather than compiler).
func f() error { ... }
try f()
Discussion Summary
This section summarizes the discussion that took place on the proposal that you don’t have to wade through lots of comments. It has been inserted into the original proposal.
The below are points that in theory are easy to resolve by altering the existing proposal:
- The name try does not capture the operation well, check and returnif are given as possible alternatives.
- The use of a comma separator is not liked by some because it looks like a multi-value return. with has been given as a possible alternative.
- Adding a ThenErr error handler function composition is dis-liked by many (it’s not clear to me why). An alternative is to not add it.
- defer try does not match how defer currently takes an expression. The alternative is to not add this.
Below are points that were raised about the benefits and costs of this proposal:
- There is some interest in generalizing these kind of proposals to work for other zero values in addition to just errors. But this has not been fully thought through. This could make the feature more powerful, and thus more worth it, or it may just be confusing and unworkable.
- It may be too much work for tooling to adapt to this new statement. Thus this may be a difficult breaking change for some tools to deal with
And of course are value judgements about whether the benefits outweigh the costs. For some Go programmers, using anything except return for returning creates multiple ways to do the same thing, which is unacceptable. Some try to address this by changing the keyword to something like returnif.
Background
Existing proposals to improve Go errors taught us that our solution must provide 2 things:
- the insertion of a return statement for errors
- compose error handler functions together before the error is returned
Existing solutions handle the first point well but most have done poorly on the second. With a slight variation on existing error handling proposals, we can provide the second.
Motivation: Go’s biggest problem
Recently the Go Developer Survey 2022 Q2 results were released. Now that Generics have been released, the biggest challenge for Go developers is listed as “Error handling / working with stack traces”.
Error handling: missing or poorly implemented in many proposals
This proposals allows the Go programmer to write the exact same code, but more tersely:
f, err := os.Open(filename)
try err
In such a simple case, with no error handler, this transformation may not be very valueable. However, even in relatively simple case, consider if the zero values are verbose:
x, err := f()
if err != nil {
return MyLargeStructName{} otherpackage.StructName{}, err
}
In the above example, programmers are tempted to return the structs as pointers just so they can return nil rather than obfuscate their code with zero values. After this proposal, they can just write:
x, err := f()
try err
Additionally, there is the case of “handling the error”. Often we want to annotate the error with additional information, at least an additional string. Adding this code that modifies the error before it is returned is what I will refer to as adding an “error handler”.
The original draft proposal solution used stacked error handlers, but this has difficulties around composition due to the automatic stacking and code readability since the error handler is invoked implicitly. A second proposal was put forth not long after which implemented try as an expression and without any support for (stacked) error handlers. This proposal had extensive discussion that the author attempted to summarize. In my view this proposal was poor because it did not create any affordances for error handling and instead suggested using defer blocks. Defer blocks are a powerful and orthogonal tool that can solve the problem, but for many normal error handling use cases they are clumsy and introduce incidental complexity.
A solution to the error problem should encourage the Go programmer to add error handling code as needed.
Extending existing solutions with function-based error handling
Composing error handlers can be solved by adding a 2nd parameter to try. The second parameter is an errorhandler of type func(error) error or more precisely with generics: type ErrorHandler[E error, F error] func(E) F.
Now we can cleanly write the following code given from the original problem statement:
func CopyFile(src, dst string) error {
handler := func(err error) error {
return fmt.Errorf("copy %s %s: %w", src, dst, err)
}
r, err := os.Open(src)
try err, handler
defer r.Close()
w, err := os.Create(dst)
try err, handler.ThenErr(func(err error) error {
os.Remove(dst) // only if Create fails
return fmt.Errorf("dir %s: %w", dst, err)
})
defer w.Close()
err = io.Copy(w, r)
try err, handler
err = w.Close()
try err, handler
return nil
}
ThenErr would be a standard library function for chaining error handlers.
The new example dramatically reduces verbosity. Once the reader understands that try performs an early return of the error, it increases readability and reliability. The increased readability and reliability comes from defining the error handler code in one place to avoid de-duping it in your mind at each usage site.
The error handler parameter is optional. If no error handler is given, the error is returned unaltered, or alternative mental model is that a default error handler is used which is the identity function type ErrorId[E error] func(err E) E { return err }
The CopyFile example is probably a best case for using defer for error handling. This technique can be used with try, but it requires named return variables and a pointer.
// This helper can be used with defer
func handle(err *error, handler func(err error) error) {
if err == nil {
return nil
}
*err = handler(err)
}
func CopyFile(src, dst string) (err error) {
defer handle(&err, func(err error) error {
return fmt.Errorf("copy %s %s: %w", src, dst, err)
})
r, err := os.Open(src)
try err
defer r.Close()
w, err := os.Create(dst)
try err, func(err error) error {
os.Remove(dst) // only if Create fails
return fmt.Errorf("dir %s: %w", dst, err)
}
defer w.Close()
err = io.Copy(w, r)
try err
err = w.Close()
try err
return nil
}
Conclusion
This proposal allows for:
- the insertion of a return statement for errors
- composition of error handler functions together before the error is returned
Please keep discussions on this Github issue focused on this proposal rather than hashing out alternative ideas. Almost all the alternatives have been hashed out already.
Provisional
This proposal should be considered for provisional acceptance. The following will need to be well-specified (some are mentioned below in the appendix):
- Decide how to interact with defer
- The best name for
try- this should be discussed separately after this proposal is provisionally accepted - Firm decision as to whether to use lazy error handlers
Appendix: alternative names
I would be happy with try being renamed to anything else. Besides other single words like check, return if has been proposed.
I use try in this proposal because it is the shortest word that has been proposed so far.
This proposal would leave it to the Go maintainers to decide the best name for the word.
Appendix: lazy error handlers
It is tempting to make error handlers lazy. This way we don’t need to bother with making curried handlers.
x, err := f()
try err, fmt.Errorf("f fail: %w", err)
I am sure this will appeal to many as seeming to be Go-like. It would work to do it this way. This proposal has a preference for the function handler over a lazy handler to reduce defects. The lazy form requires using an intermediate variable 3 times. It is possible in Go to produce a defect by using the wrong error variable name.
A go program generally only needs 3 supporting standard error handling functions in a curried form.
- Wrap an error so that it can be unwrapped (%w)
- Wrap an error so that it cannot be unwrapped (%v)
- Add a cleanup handler
However, we should consider supporting both a lazy handler and a function handler.
Appendix: special usage with defer
We could explore making the defer and try combination special in that it would accept an error handler function and apply it to the returned error value (if not nil) without requiring a named return value
func CopyFile(src, dst string) error {
defer try func(err error) error {
return fmt.Errorf("copy %s %s: %w", src, dst, err)
}
Appendix: Citations
- https://go.dev/blog/survey2022-q2-results
- https://go.googlesource.com/proposal/+/master/design/go2draft-error-handling-overview.md
- https://go.googlesource.com/proposal/+/master/design/go2draft-error-handling.md
- https://github.com/golang/go/issues/32437 - the original try error proposal
- https://github.com/pingcap/errors - a library for adding stack traces to errors
- https://github.com/pingcap/errcode - a library for error codes via interfaces
- https://github.com/gregwebs/try - a library that tries to implement this proposal in user code
Appendix: prior art
There are 2 boilerplate reductions from this proposal:
- avoiding
if err != nil {and using 1 line - avoiding generating zero values for the return statement
I believe the latter is well addressed by this proposal that automatically generates zero values from return ..., err. It is unfortunate that no action has been taken on that existing proposal. If this proposal were accepted, I think that in any place where one might use return ..., err one could just use try. If return ..., err were already possible I think try might not add enough value.
This proposal is still open and is equivalent to this proposal without the handler argument. It is suggested to add error handling via handler functions that already have an if err == nil { return nil } guard. But then using handlers requires reading code and looking at the calling function call to understand how it works and to ensure that it works properly.
There have been proposals for dispatching on or after an error value assignment. These are quite similar to this proposal but suffer from being tied to assignment.
- https://github.com/golang/go/issues/52416
- https://github.com/golang/go/issues/42318
- https://github.com/golang/go/issues/46655
- https://github.com/golang/go/issues/36284
- https://github.com/golang/go/issues/32884
This proposal is different, but notes that it adds a with keyword for the handler. We could do that for this proposal, but it seems preferable to only reserve one keyword and use a comma.
I made a similar proposal in which try was an expression rather than a statement.
Appendix: generic enumerations
Now that Go has Generics, we might hope for this to get extended to enumerations and have a Result type like Rust has. I believe that when that day comes we can adapt try to work on that Result type as well.
Appendix: implementation
The try library implements this proposal as a library function. However, it has several shortcomings as a library function that can only be resolved by building features into the Go language itself.
Appendix: code coverage
There are concerns about code coverage. It may be a significant burden for line-oriented code coverage tools to figure out how to tell users if the error paths are getting exercised. I would hate for a helpful tool to hold back back language progress: it is worth it for the community to undertake the effort to have code coverage tools that can determine whether the error path of try is getting exercised.
Appendix: examples
import (
"fmt"
)
// This helper should be defined in the fmt package
func Handlew(format string, args ...any) func(error) error {
return func(err error) error {
args = append(args, err)
return fmt.Errorf(format+": %w", args...)
}
}
// This helper should be defined in the fmt package
func Handlef(format string, args ...any) func(error) error {
return func(err error) error {
args = append(args, err)
return fmt.Errorf(format+": %v", args...)
}
}
func valAndError() (int, error) {
return 1, fmt.Errorf("make error")
}
func newGo() (int, error) {
x, err := valAndError()
try err
// Common formatting functions will already be provided
i := 2
x, err = valAndError()
try err, Handlew("custom Error %d", i)
// Using a custom error type
// For convenience the error type can expose a method to set the error
x, err = valAndError()
try err, TheErrorAsHandler(i)
}
type TheError struct{
num int
err error
}
func (t TheError) Error() String {
return fmt.Sprintf("theError %d %v", t.num, t.err)
}
func TheErrorAsHandler(num int) func(err) TheError {
return func(err error) TheError {
return theError{ num: i, err: err }
}
}
Appendix: real world code base examples
I did some automated language transforms to use try on the golang codebase. This is easily automated now with Semgrep rules and a little shell script so I could apply this to any code base. Unfortunately it is only examples of using try without an error handler. Try with an error handler is much more difficult to automate.
Costs
- Would this change make Go easier or harder to learn, and why?
Harder to learn the language spec because users must learn a new keyword. However, when verbose error handling code is removed, beginners will be able to read and evaluate Go code more quickly and learn Go faster.
- **What is the cost of this proposal? **
The costs are discussed in detail elsewhere
- understanding a new keyword
- requiring
go fixfor upgrading - code coverage tool upgrading
- How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected?
All, but those that re-use Go libraries may just need to upgrade their library usage? I think these tools would then have less source code to analyze and thus run more quickly. Linters that check that all errors are handled currently use a lot of CPU. These could be simplified or even removed entirely.
- What is the compile time cost?
Without handlers I would think it could be reduced because the compiler can avoid evaluating code that previously would have been hand-written. It will reduce the error linting time more significantly (see above) for those of us that run linters right after compilation since checking for proper error handling will be easier.
- What is the run time cost?
I think this should be equivalent. The success and error path should be the same. However, having better error handling abilities will encourage Go programs to better annotate their errors. But the error path should not be a performance concern.
- Can you describe a possible implementation?
I started a branch that gives some idea of some of the changes required, but keep in mind that it is incomplete and already making implementation mistakes.
- Do you have a prototype? (This is not required.)
This can be roughly implemented as a library, done here. However, it is limited and that can only be solved with compiler modifications. Internally it uses panic/recover.
- This is slow when there is an error (but fortunately does not affect the success path)
- It requires a
deferat the top of the function and using a pointer to a named return variable for the error
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 339
- Comments: 57 (6 by maintainers)
Commits related to this issue
- example of using a try builtin for error handling as per https://github.com/golang/go/issues/56165 This was automated with a Semgrep rule and sed. It only demonstrates the form of try without an erro... — committed to gregwebs/go by gregwebs 2 years ago
- remove try.TryX and try.CheckX functioncs try.Try and try.Check can be used. This just requires an intermediate error variable. In many real world cases this is a good way to make code readable. This... — committed to gregwebs/try by gregwebs 2 years ago
There is no try only do. Go errors are explicit, verbose and on the nose. If you hide it behind magic, you will have a bad time. If I read the code, I can clearly identify an error and what will happen if said error does occur. I don’t want to hide it! I want to see, read and be reminded of its existence. Otherwise, I might miss an important error.
Also, I’m adding different error messages for each error with
fmt.Errorf. If I understand you correctly, I would have to have a different handler for each of those? That would obviously be a problem and just increase the complexity and number of lines further. 😃I would love to have
try, but as block and only to simplify existing Go error handling.Inside try block, every non-nil assignment to err jumps to except and finally blocks.
Something like this:
I
Thanks for the proposal.
I am fully against it:
But, above all, it steepens significantly the learning curve:
Learning case one: erros are values, errors are there when a non nil value is detected
Learning case two: a special keyword, try, is introduced, that behaves in the following way: case 1… case 2… case 3, and code is autogenerated…
Basically it boils down to targeting people coming from Java/Javascript, where it’s easier, in the beginning, to read code that has “try”, so we obfuscate a clean language to make the learning curve shorter by one day.
Thanks for the proposal. My 2 cents: If
try erris translated toreturn err, why writetry err, handlerto have it translated toreturn handler(err)? We could also writetry handler(err), which is just cleaner and more understandable, and it could be lazily evaluated like adeferstatement, so that the handler is not gonna be called if there is no error.This is a great discussion all, clearly something a lot of people are interested in. I’ve been following along here and it seems we all just want less clutter for the most common use cases of error handling. So what if we took a little bit out of JavaScript’s playbook and even made it more terse:
if errwould obviously evaluate toif err != nil@steerling thanks for pointing that out. Just this week I wrote a bug where I messed up the equality and wrote
if err == niland it got through code review. Granted, I am working to setup linters that will catch that, but ideally we wouldn’t need to rely on linters. The point is that rather than being highly readable, it ends up being a bunch of boilerplate that is glazed over. However, the!=nilis not as bad as having to insert zero values. Developers are tempted to return pointers to structs just so they can have something that is easy to write and readreturn nil, nil, errrather thanreturn LongStructName{}, package.OtherStructName{}, errwhere the zero values are reducing readability.I strongly believe we should not add the
ThenErrerror chaining in this proposal. Better error handling has been discussed a lot, so I’d call this a big change to the language. I’d recommend we keep the change small for its first iteration.Some other things that stick out to me:
checkorreturnifis a better keyword. “Try” implies that you are trying something, but an error might occur. With this proposal, the “trying” has already occurred, and we’re simply checking if an error has occurred.deferthat interacts with named returns error params, although if the handler is just syntactic sugar forif err != nil {...}, then defer’s should continue to work as normalI believe we put all the language changes for error handling on hold. We should probably do the same for this one.
A few people now (across this and some earlier proposals) have proposed alternatives which seem to involve combining the following two independent building-blocks:
errorin a context where boolean values are expected and have it convert totrueif the error is non-nil andfalseif it’s nil. There have been various proposals that would amount to this before:I think it’s instructive that of all of these at the time of writing only the second set remains open, and in particular the first set related to using
error(or any other subset of nilable types) as a predicate seems to have been explicitly declined multiple times and is therefore not on the table unless there is new information to consider.I think the shortest possible alternative using the above ideas, without using any mechanisms that have already been declined in previous proposals, would be something like:
or indeed, the one-liner form of that:
…but that’s already essentially what #21182 proposes – the above uses nothing except a form of that proposal – and so that other issue is probably the better place to discuss the pros and cons of that approach, rather than here.
@gregwebs thanks for writing this proposal, even though I disagree with it.
I think this bug is a bad argument for
if err != nilbeing a negative thing. This bug is a logic bug that should have been caught with a test. Errors being values and treated as regular code makes this simple.A few general thoughts and opinions on the proposal.
This proposal arises from
perspective of the code writer
IMO the perspective of the code reader is more important in programming languages than the perspective of the code writer. It is better for the writer to write a few more characters than for the code to be harder to read. I believe that current syntax would not have persisted if it were not easy to read.
A quote from Uncle Bob on the idea of writability versus readability:
the habit to see in other programming languages an additional syntax for error handling.
I like
?at the end of calls that could return an error. For example:expands to
I wrote a dumb script for my own use:
I am a beginner. My proposal is 99% certain to be stupid
what abt
have this been tried to be probosed before (i haven’t been keeping up)?
I didn’t read all the posts, but like others I though and read a lot about error handling in Go, I don’t have strong opinion, more thinking loud.
Is it really about adding a new keyword because of a missing functionality or is it about code format/readability?
if we can add a case to the go formatter to allow one line
ifwith couple of statements like thisborrowing from @gregwebs post above
Anyway, if error handling is more than one line beside
returnit should be in multiple lines, this way it explicitly describe the steps of error handling.This proposal is rather inconsistent in the definition of try’s grammar. At the start of the proposal try’s arguments are introduced as “error and handler of type func(error) error”, but later in “Appendix: lazy error handlers” try is used with arguments of “error and function call”.
Additionally, in my opinion, this proposal is much more costly, that the author suggest. It’s not only the keyword to learn, but also a difference of when to use
if err != nilidiom and when to use this syntax sugar. I believe this proposal may either introduce inconsistency in error handling, since one times theif err != nilwould be used and other times thetrystatement with a handler, it won’t be possible for go vet to automatically figure out, which fits the best, or totally revolutionize the current approach at error handling. Moreover, it definitely introduces inconsistencies in ways of exiting a function, which would require a massive rework of all tools, that rely on analyzing that.I disagree that this proposal makes anything more “magical” or “hides” anything. This proposal provides syntactic sugar to handle an extremely common usecase and the final result is clearer more succinct code. I am personally okay with the current
if err != nilway of error handling but this is indeed seen as somewhat of speedbump by many go developers.It must be noted that this proposal maintains the current spirit of Go’s error handling- the errors remain explicit and one is free to drop into
if err != nilblocks if that makes more sense in the current context while providing syntactic sugar for common usecases. Rust for example also provides a construct that is similar in spirit to what this tries to accomplish and it does not result in code being any less explicit or more magical.If this is an essence of the past error proposals, you could make it easier for the occasional reader of go code, if you borrow the “return if” naming from proposals like #53017. The
trystatement from this proposal could simply be renamed toreturnif:This would better indicate that this is a return statement, and still allow to grep for “return” through the source code.
If you would addionally allow to infer the ‘!= nil’ comparision for values of type error in if statements, the operation could be made even more obvious. Then the
returnif errstatement could be a shorthand forreturnif err != nil, err. And this would just be syntatic sugar for a checked return statement with zero value insertion:@apparentlymart thank you so much, I value your feedback and the time you have taken to properly understand the proposals. And I really appreciate how you have separated it out into 2 sections.
I think I will alter the proposal to put the content on
deferinto an appendix section. It’s definitely the part where I am thinking out loud about something I don’t have a great conclusion on yet.In that case is is possible to do this transformation in the future then? But when I look to the future I hope for a language that has sum types and is using a result type and an option type. And I think this would only need to work for those two types. So attempting to generalize now to zero types may cause problems in the future.
As for the details not affecting acceptance:
I actually prefer
checkas well, but have stuck withtryfor now because it is fewer characters.What do you think about other possible options to replace a comma:
Thanks for writing up this refined proposal, @gregwebs!
Although I have personally become accustomed to the “long form” of error handling in today’s Go and don’t feel a strong need to reduce it, I do also recognize that many other Go programmers – including those new to Go – find the current approach rather jarring/uncomfortable. Of all of the proposals for first-class error-flow-control constructs so far, this one is the one I feel most comfortable with because I can easily follow what it’s a shorthand for, without having to develop any complex new mental models once I’ve learned about the new keyword.
Some initial reactions below. Some of these are just clarifications to make sure I understood correctly, while others are actual feedback.
Although framed as being for errors, on the surface it seems like this can be defined in a way that doesn’t special-case the
errortype:The expression which appears after
trymust be assignable to the last return value of the function. If activated, thetrystatement assigns that expression to the last return value and updates any other return values to be the zero value of their type.One hitch with this, though: if it’s broader than error then the implied
ifpredicate is trickier to define, because not all types can be meaningfully compared tonil. Perhaps it could be defined as accepting anything which can be compared tonil, although that seems to get into non-orthogonal territory. Perhaps it is better after all to requireerror, if only because there’s a well-understood convention for whether anerrorvalue represents an error or not.The
defer tryvariant feels a bit tricky. Defer is currently defined as being followed by an expression, but we’re intentionally makingtrya statement instead of an expression and sodefer tryis essentially a new statement in its own right here, which I see you already touched on in the proposal.Making
defer trywork seems like would invite questions about whydefer ifdoesn’t work, iftryis a shorthand for anifstatement. However, I don’t personally feel that strongly about it and am only mentioning it for completeness; I can’t imagine any real situation wheredefer ifwould be clearer than a normalifstatement inside adeferfunction.I retain my previous worry that the form of
trywith only one expression is so convenient (in comparison to the longer version which requires defining a separate error-transforming function) that folks will be tempted to use it in situations where it isn’t ideal.However, I can’t really argue that this isn’t already somewhat true for
if err != nil { return err }. The fact that the expression in thereturnstatement is an inline “lazy expression” (to copy the terminology from your proposal) does make it marginally more convenient, but as you pointed out it would be possible fortryto treat its second argument that way if that turns out to be a significant concern.I think the above is the extent of my initial substantive reaction. Everything else is details that I would not expect to affect the acceptance of the proposal. The following is therefore intended as presumptuous feedback for a subsequent effort to develop this from an accepted proposal into a final design, rather than for deciding whether to accept the proposal:
tryfollowed by a noun rather than a verb just reads weirdly to me. I findcheckbetter because it is a word that seems more naturally followed by a noun. I also like the idea of something involvingreturnif possible, since that would really cement the idea that this is a shorthand for returning early.(
try DoSomething()for a function which only returns an error doesn’t have this problem; it’s the form where we’re checking anerrorvariable assigned in an earlier statement that I’m concerned about.)Using a comma between the error expression and the error transformer expression feels a bit too similar to the syntax for multiple return values or multiple assignment. I worry that a naive reader would presume that the second argument is somehow another value to be returned, rather than a function to be called. I’d recommend either another keyword or some different punctuation, to hopefully make it more obvious that the error transformer is something unique to this statement.
Thanks again!
maybe we just need a auto return keyword when error occred.
@ianlancetaylor as a side note I think the model of using an issue as a proposal is broken. Proposing via pull request (as is commonly done in community RFC processes) would be a big improvement because it would allow for and encourage a clear discussion thread on a particular point of a proposal.
It doesn’t seem there is any new discussion on this proposal (instead just different proposals). The different proposals being posted here have all been proposed before.
Let me try to summarize the feedback on this proposal. If someone wants to chime in with major points that I missed, that would be appreciated. Otherwise I would be happy to close the discussion now.
The below are points that in theory are easy to resolve by altering the existing proposal:
trydoes not capture the operation well,checkandreturnifare given as possible alternatives.withhas been given as a possible alternative.ThenErrerror handler function composition is dis-liked by many (it’s not clear to me why). An alternative is to not add it.defer trydoes not match howdefercurrently takes an expression. The alternative is to not add this.Below are points that were raised about the benefits and costs of this proposal:
And of course, there are a lot of value judgements about whether adding this new feature is worth the cost or otherwise the right thing to do for Golang. In the above I tried to write down any factual statements that came out of these, but sometimes they are hard to tease out.
Thanks for taking the time to make a proposal @gregwebs , while this would make it easier for newcomers, it is not to my taste. For some reason I don’t see this as “go” code. It seems to me that the solution you propose would only remove 1 line
if err != nilwhile adding more lines for thehandler(err)func in the end.I like the idea of improving error handling, but to me, having
try err, h(err)sprinkled in my code instead ofif err != nilisn’t great. I’d stick with the old way if I had the option.Sorry I’m not more helpful, but I can’t even nail down why I don’t like it, maybe it reminds me too much of
?hidden error returns, or flashbacks of Python’s exceptions… 😄I would much rather see something akin to Rust’s
Result<T, E>andOption<T>handling of errors and optionals, backed by the type system. I think those would be much more valuable, even though they would require more changes to the language.Speaking personally, I find the
if err != nilclutter to decrease readability, so this proposal would help with that (for me at least)@m3talsmith Thank you for helping to bring the tone of the conversation back to where it started
ThenErris not required. It isn’t a builder pattern, it is a specialization of function composition. The point is really just that we can compose functions to stack handlers. There are different ways to achieve consumption. In my try library I allow additional handlers as extra arguments.with respect to your example, I don’t quite understand it. It looks like you just got rid of the nil check, which has been proposed in the past. We are trying to keep the discussion centered on this proposal and small changes that could be made to it rather than different proposals.
@earlye there is already a proposal open that is the same as this but without error handler. In the appendix of my proposal and as a comment on that proposal I explain why error handlers should be first class.
@Skarlso I agree and I think that’s the idea behind the “lazy” appendix, which I think would be essential to make this useful.