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: Go2: rewrite APIs that use integers for signaling errors #22792

Closed
hirochachacha opened this issue Nov 17, 2017 · 3 comments
Closed
Labels
FrozenDueToAge Proposal v2 A language change or incompatible library change
Milestone

Comments

@hirochachacha
Copy link
Contributor

Unlike C, it seems to me that Go encourages to use multi-value to signal errors when the function fails.
However some standard APIs are not comformance to the theory. (strings.IndexXXX, sort.SearchXXX, ...)
Those APIs enforce developers to learn extra knowledge before using, which is not predictable.

I propose rewriting those APIs.

For instance,

func Index(s, substr string) int
func Search(n int, f func(int) bool) int

will be

func Index(s, substr string) (int, bool)
func Search(n int, f func(int) bool) (int, bool)
@gopherbot gopherbot added this to the Proposal milestone Nov 17, 2017
@bradfitz bradfitz added the v2 A language change or incompatible library change label Nov 17, 2017
@docmerlin
Copy link

docmerlin commented Dec 2, 2017

I like the old behavior. It makes for easy error checking in loops, so you don't have to unpack multiple values awkwardly. Adding the bool seems unnecessary to me, when =-1 will turn the result into a bool. Furthermore, these functions have -1 as "could not find" in most languages I have encountered, so it makes it easier for people coming from other languages.

@hirochachacha
Copy link
Contributor Author

Adding the bool seems unnecessary to me, when =-1 will turn the result into a bool. Furthermore, these functions have -1 as "could not find" in most languages I have encountered, so it makes it easier for people coming from other languages.

That theory doesn't explain sort.Search and its family.

https://golang.org/pkg/sort/#Search

If there is no such index, Search returns n. (Note that the "not found" return value is not -1 as in, for instance, strings.Index.)

The note implies that always assuming -1 as the "not found" value is a common mistake.

If we can make type signatures more descriptive, we won't see those kind of mistakes.

@ianlancetaylor
Copy link
Contributor

The functions as they are today work fine. Rewriting will make many uses more complex, as it will be necessary to use variables in a separate statement rather than just an expression. Currently these functions return an invalid index on failure, and using it will fail. If we change to the standard convention, we would return zero on failure, which is a valid index. We would probably instead prefer to continue to return an invalid index, making it more complicated. There are arguments on both sides, so let's keep things they way they are.

@golang golang locked and limited conversation to collaborators Mar 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

5 participants