-
Notifications
You must be signed in to change notification settings - Fork 18k
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: reflect: add support for Equal(T) bool
method in DeepEqual()
#20444
Comments
I assume this is a proposal? If so, please use a I'm concerned about the case where an underlying type implements In general it seems like a good idea, though. |
Equal(T) bool
method in DeepEqual()
Equal(T) bool
method in DeepEqual()
done
I think that if a method |
I'm opposed to adding this functionality to I understand that this is probably solving a real problem. For example, many usages of However, these are why I don't believe
In my opinion, the right solution is an entirely new API (possibly a new package), that provides that equality for use in tests. Not only should this new equality function call |
I was reading the source code, in order to (maybe) write my own package, and I found something strange. |
@pierrre it looks like you could indeed simplify it - just send a CL. If it's like that for some reason, there should be a comment clarifying. And any person who can approve the CL will let you know if the change is wrong. |
FYI |
Other exceptions:
|
@dsnet I just understood what you mean by
(Sorry, I'm not a Golang reflect expert) https://golang.org/pkg/reflect/#Value.Call documentation isn't very clear about unexported fields. I'm withdrawing my proposal and closing the issue. Maybe I'll write my own library... |
For anyone interested, I've built this https://github.com/pierrre/compare |
I believe this was built simultaneously: https://github.com/google/go-cmp |
Currently
reflect.DeepEqual()
compares values very simply.It causes issues with types that implement their own equality logic, like
time.Time
.I propose to add a check for types that have a
Equal(T) bool
method.I think it's better to call the
Equal(T) bool
method if it's implemented, rather than relying on the default implementation.Several types could benefit of this change (see https://golang.org/search?q=Equal ):
time.Time
net.IP
regexp/syntax.Regexp
The change is very simple:
Add
before the large
switch v1.Kind()
block.(I'm not very familiar with the reflect package, so my code may contain errors).
The text was updated successfully, but these errors were encountered: