You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
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.
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.
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,
will be
The text was updated successfully, but these errors were encountered: