Skip to content
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: OR operator on error return #41539

Closed
Codebreaker101 opened this issue Sep 21, 2020 · 5 comments
Closed

proposal: Go 2: OR operator on error return #41539

Codebreaker101 opened this issue Sep 21, 2020 · 5 comments
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@Codebreaker101
Copy link

Sometimes I have a function that does two or more things and I need to return an error if something failed in the function.

To do that I have to do the following:

func do() error {
	if err := doSomething1(); err != nil {
		return err
	}

	if err := doSomething2(); err != nil {
		return err
	}

	if err := doSomething3(); err != nil {
		return err
	}
}

Wouldn't it be nice if once can do this insted:

func do() error {
	err1 := doSomething1()
	err2 := doSomething2()
	err3 := doSomething3()
	return err1 || err2 || err3
	/* OR */
	return doSomething1() || doSomething2() || doSomething3()
}

Opinions?

@gopherbot gopherbot added this to the Proposal milestone Sep 21, 2020
@earthboundkid
Copy link
Contributor

See #37165 which would work this way.

@zigo101
Copy link

zigo101 commented Sep 22, 2020

func AnyError(errs ...error) error {
   for _, e := range errs {
      if e != nil {
         return e
      }
   }
   return nil
}
...

return AnyError(err1, err2, err3)



return AnyError(doSomething1(args), doSomething2(args), doSomething3(args))

It would be greater if lazy evalutaion is supported.

@martisch martisch added v2 An incompatible library change LanguageChange Suggested changes to the Go language labels Sep 22, 2020
@martisch martisch changed the title proposal: OR operator on error return proposal: Go 2: OR operator on error return Sep 22, 2020
@martisch martisch added the error-handling Language & library change proposals that are about error handling. label Sep 22, 2020
@martisch
Copy link
Contributor

For language change proposals, please fill out the template at https://go.googlesource.com/proposal/+/refs/heads/master/go2-language-changes.md .

When you are done, please reply to the issue with @gopherbot please remove label WaitingForInfo.

Thanks!

@martisch martisch added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Sep 22, 2020
@gopherbot gopherbot removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Sep 22, 2020
@martisch
Copy link
Contributor

See #40432 for past error handling proposals and associated design problems.

@martisch martisch added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Sep 22, 2020
@Codebreaker101
Copy link
Author

As mentioned above, there are open issues that describes this proposal, in one way or another. Closing this.

@golang golang locked and limited conversation to collaborators Sep 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants