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: spec: named check-handle pairs for error handling #31743

Closed
ghost opened this issue Apr 29, 2019 · 3 comments
Closed

proposal: spec: named check-handle pairs for error handling #31743

ghost opened this issue Apr 29, 2019 · 3 comments
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@ghost
Copy link

ghost commented Apr 29, 2019

After reading the proposed draft design I was very glad that there are some upcoming changes to the errorhandling in go.
I have also read a lot of similar proposals and you may argue this is a clone of some other proposal. My intention behind this is to offer my oppinion as a scriptingwise go-dependant engineer. Speed, readability and reusability are very much the things I am looking for.
The general idea behind check and handle will address the current issues with readability very well.

The example in the draft design explains the usage of check and handle:

func printSum(a, b string) error {
	handle err { return err }
	x := check strconv.Atoi(a)
	y := check strconv.Atoi(b)
	fmt.Println("result:", x + y)
	return nil
}

This adresses readablility a lot, but repetitive code not by much:

  • to use a different handle you would have to redefine it each time
  • there would be at most one default handle

Named dicts or structs are being used in various languages.
As an addition to check handle I am now proposing - similar to labled handles - named pairs of these keywords. A named pair would have the following syntax:
handle.<name>
check.<name>
Rewriting the given example you could then name the handle:

func printSum(a, b string) error {
	handle.chain err { return err } 
	x := check.chain strconv.Atoi(a)
	...
}

I know that that assigning to an attribute of a keyword like handle.<name> is unwanted by most and this needs to be adressed, but this code would be very readable and very reusable. There could also be standard definitions of named pairs like check.chain, check.panic, etc. or exported handles like handle.Chain or Handle.Chain.
I believe this would integrate very well with the current go language and would make at least my life easier.

Thanks for reading! I'm looking forward to the upcoming changes!

@gopherbot gopherbot added this to the Proposal milestone Apr 29, 2019
@networkimprov
Copy link

Please add a link for this to https://github.com/golang/go/wiki/Go2ErrorHandlingFeedback

Also, if you haven't, you might consider how your ideas fit into the broad range of possible requirements I've assembled in Requirements to Consider for Go 2 Error Handling.

@rsc rsc added the v2 A language change or incompatible library change label May 7, 2019
@rsc rsc changed the title Proposal: Named check handle pairs proposal: spec: named check-handle pairs for error handling May 7, 2019
@tv42
Copy link

tv42 commented Jun 9, 2019

That's a novel and confusing meaning for dot. I think this proposal is more likely to get a fair shake if it stays within the existing syntactical style of Go.

If you used parens, you'd stay closer to Go style, though with added noise. I'm also doing handles a little differently, making it be more like func.

func printSum(a, b string) error {
	handle chain(err) { return err } 
	x := check(chain) strconv.Atoi(a)
	...
}

If you made the name required, you wouldn't need the dot-separator at all. Though simple uses of check might be better implicitly using the last unnamed handle.

func printSum(a, b string) error {
	handle chain err { return err } 
	x := check chain strconv.Atoi(a)
	...
}

@ianlancetaylor
Copy link
Contributor

Thanks for the proposal.

As @tv42 says, the dot syntax used here is very unlike any other use of dot in the language. It might be interesting to try to implement this using a more label-like syntax.

This doesn't address the issues of the earlier design: it's not clear how handlers and the defer keyword interoperate. It's not clear how or whether handlers chain.

We don't currently expect to implement the earlier handler proposal. Closing this one. We can reopen this if we return to the earlier idea.

Writing for @golang/proposal-review .

@bradfitz bradfitz added the error-handling Language & library change proposals that are about error handling. label Oct 29, 2019
@golang golang locked and limited conversation to collaborators Oct 28, 2020
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 Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

6 participants