-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: errors: explicit return type for errors.Join #70989
Comments
#47811 looks like a similar proposal, but I feel like this proposal is much smaller in size/scope. |
It is not possible to change the return type like this because it is an observable breaking change. err := errors.Join(errs...)
err = fmt.Errorf("some context %s: %w", str, err) it'll fail to build if we change the return type because the inferred type for |
@Jorropo I am sorry, but I don't entirely follow. Are you saying that making |
The only critical problem is changing the return type of The problem I am demonstrating is not with err := errors.Join(nil)
var errr error
err = errr this would compile before your change but not after. Tl;Dryou can't change the signature of
|
closing as not feasible |
Proposal Details
I have a use-case where I would like to signal that a function of mine can return multiple errrors such that I can iterate over them. Returning multiple errors is a common case for validation errors.
I could obviously return
[]error
, but that does not entirely feel like canonical Go to me. I could also implement my ownerrors.Join
where I simply return my own type that implementsfunc (t multiError) Unwrap() []error
. However, in the spirit of "Accept interfaces, return concrete types", I am wondering whether it would it make sense to change the signature oferrors.Join
fromto
where
MultipleErrors
implements theerror
interface (ie. fully backwards compatible change) and is identical tojoinError
.With this change in mind, a function would be able to signal that it returns multiple errors simply by looking at its interface. For example:
The text was updated successfully, but these errors were encountered: