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

Comments

@Codebreaker101
Copy link

@Codebreaker101 Codebreaker101 commented Sep 21, 2020

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
@gopherbot gopherbot added the Proposal label Sep 21, 2020
@carlmjohnson
Copy link
Contributor

@carlmjohnson carlmjohnson commented Sep 22, 2020

See #37165 which would work this way.

@go101
Copy link

@go101 go101 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 changed the title proposal: OR operator on error return proposal: Go 2: OR operator on error return Sep 22, 2020
@martisch
Copy link
Contributor

@martisch martisch commented Sep 22, 2020

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
Copy link
Contributor

@martisch martisch commented Sep 22, 2020

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

@Codebreaker101
Copy link
Author

@Codebreaker101 Codebreaker101 commented Sep 22, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.