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: check handling support for the "comma ok" idiom for maps #30332

Closed
ghost opened this issue Feb 20, 2019 · 3 comments
Closed

proposal: Go 2: check handling support for the "comma ok" idiom for maps #30332

ghost opened this issue Feb 20, 2019 · 3 comments

Comments

@ghost
Copy link

ghost commented Feb 20, 2019

https://golang.org/doc/effective_go.html#maps

For instance this:

func Foo(info map[string]interface{}) error {
	baz, ok := info["baz"]
	if !ok {
		return ErrMissingKey
	}
	qaz, ok := info["qaz"]
	if !ok {
		return ErrMissingKey
	}
	qux, ok := info["qux"]
	if !ok {
		return ErrMissingKey
	}
	return Bar(baz, qaz, qux)
}

Would reduce to this:

func Foo(info map[string]interface{}) error {
	handle err { return ErrMissingKey }
	return Bar(check info["baz"], check info["qaz"], check info["qux"])
}

Foo then inlines or factors out, with significant improvement in both readability and code size.

IMHO, unification with the draft behavior would be intuitive.

A check may also apply to an expression of type bool or a function call returning a list of values ending in a value of type bool. If the bool is false, check invokes the current handler with an error value of nil.

I have to admit, the translation of a false bool to a nil error is ugly. However, it would be very convenient.

@gopherbot gopherbot added this to the Proposal milestone Feb 20, 2019
@ghost
Copy link
Author

ghost commented Feb 20, 2019

I think this is probably a bad idea.

@ghost ghost closed this as completed Feb 20, 2019
@networkimprov
Copy link

You're not the first to raise this possibility! It appeared in a comment on a gist among the counter-proposals on Go 2 Error Handling Feedback.

And I've listed it in Requirements to Consider for Go 2 Error Handling.

@networkimprov
Copy link

@gopherbot add Go2
@gopherbot add LanguageChange

@golang golang locked and limited conversation to collaborators Feb 25, 2020
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants