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
reflect.DeepEqual on two empty slices returns false #42265
Comments
|
|
Oops, Ian posted at the same time. |
I should further note that this is documented on
If you need a fuzzier comparison for slices, consider using the import "github.com/google/go-cmp/cmp"
import "github.com/google/go-cmp/cmp/cmpopts"
cmp.Equal(x, y, cmpopts.EquateEmpty()) |
How is that possible when both slices have length of 0? |
A See the discussion of |
Wow. So printing it gives "[]". Getting length gives 0. But comparing them yields false. That is quite a surprising edge case. I'm new to Go obviously but that's really surprising behavior. Far too late now to do anything about it I suppose. Thanks. |
Actually, I guess DeepEqual isn't quite what I want here but it was suggested by go itself when I tried to use == on this type. Perhaps that hint can be updated to suggest what you recommended instead? |
Another idea for making Go more friendly is to improve DeepEqual's docs to mention this edge case and also mention alternative methods. |
Where did that suggestion come from?
The |
The edge case I had in mind was actually that len(nil) == 0 for nil slices. That is not mentioned. That seems to be an inconsistency: we're willing to pretend nil is the same length but not the same equality. |
I suppose I don't think it's necessary for |
Sure. I get that. I'm only suggesting ideas for helping newcomers to Go (like me). Surely it's documented somewhere. But in many languages you can use |
I just don't think it is the place of the The implementation of |
I opened #42272 to make it clearer on Read()'s end since you could also claim that the documentation burden is on that side. In fact, the reviewer of that says that it's the standard library's custom to treat |
Yes, which is why it is explicitly documented in the |
I understand that this area is a source of confusion for new Go programmers. And I understand that it was confusing for you in particular. And if there is good way for us to avoid that confusion, we should definitely do it. But I am not at all convinced that what we need to do is explain in the |
Agreed. I was more thinking of a "gotchas" section or something like that. Something that communicates the incongruity like: "NOTE: Unlike |
I can't even promise that such a wording would have helped me. But what I didn't understand is that everything treats |
Notably, writing your own version of I'm saying all of this only because I suspect I'm not the only person who has or will run into this. |
The |
Fair enough. I guess I was just too new to Go to make sense of that correctly. |
What did you expect to see?
reflect.DeepEqual
on two slices with the same type and same length of0
should be true.What did you see instead?
See repro here: https://play.golang.org/p/YjCo7OcS7hv
The text was updated successfully, but these errors were encountered: