-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: errors: Is resolve nil error problem #40674
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
Comments
Confusion about whether storing a Also, given that the language is careful to say that a |
I agree with @ianlancetaylor that changing this would only add to the confusion. It is a very deliberate choice that |
Agreed, though I like the idea of trying to clean up this thorn for new users, as we see it internally every now and then. Plus it tends to be weirdly hard to debug and hunt down in my experience. After some additional thought, an intuitive way to implement this would be to map
Correct, we would need to document that |
I can't see what the difference is between this issue and #40673. 40673 is: proposal: errors: allow implementing err.Is(nil) == true It's clear to me how those two are different. But isn't this one the same as 40673? |
No, they are different. Though it may be better to think of them as complementary. This one only intends to solve the nil error problem called out in the faq: type MyError struct{}
func (*MyError) Error() string { return "" }
func main() {
fmt.Println(errors.Is((*MyError)(nil), nil))
// Output: true
} During the discussion, it was pointed out that only using the default value of nillable things (pointers, maps, slices, channels) was confusing, so the option to extend this to all default value types was added: type ErrorString string
type ErrorStruct struct{}
type ErrorArray [0]struct{}
func main() {
fmt.Println(
errors.Is(ErrorString(""), nil)
errors.Is(ErrorStruct{}, nil)
errors.Is(ErrorArray{}, nil)
)
// Output: true, true, true
} |
I've read this multiple times and am still not completely sure what you are proposing here. Do I have that right? (If |
Based on the conversation above, this seems like a likely decline. |
Yes, that is correct. There are different flavours (just nil for pointers, nil maps too, any default value), but you have the gist. (This does not refer to the implementation of This is not a breaking change. |
Telling everyone that all their code using |
While I agree we cannot mandate rewriting all the code, I had assumed that we were already doing that with That being said, as long as old code continues to compile (and run), it is not a breaking change to suggest that an old syntax, |
Declaring that Let's move on. |
No change in consensus, so declined. |
background
A long standing problem with error handling, but really checking
nil
ness of interface variables, is typed nils:description
Since we already introduced a function than handles error comparisons,
errors.Is
, we could resolve this edge case by checking botherr == nil
and underlying valuenil
ness:costs
This will make
errors.Is
more complex, and meant that developers should eschewerr != nil
in favour of!errors.Is(err, nil)
. At the same time, we already suggesterrors.Is(err, ErrBad)
overerr == ErrBad
, so this would serve to make the language more consistent.alternatives
It may also be confusing that this only applies for
nil
able types. We could instead define that all default value types arenil
:Since every implementer may want to handle the nil case differently, we could instead upon the implementation of
interface{ Is(error) bool }
to determine what should be done. This is a larger topic so see #40673. That being said, for errors that are implemented upon types, e.g.func (MyError) Error() string
, we may need to have a standard resolution for(*MyError)(nil)
. Furthermore, if #40673 is accepted, we should allow it to override any defaults suggested here.Since we are flattening
nil
ness in theerr
parameter, should we allow the same in thetarget
,errors.Is(err, (*MyError)(nil))
?The text was updated successfully, but these errors were encountered: