Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cmp/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ import (
//
// - If the values have an Equal method of the form "(T) Equal(T) bool" or
// "(T) Equal(I) bool" where T is assignable to I, then use the result of
// x.Equal(y) even if x or y is nil. Otherwise, no such method exists and
// evaluation proceeds to the next rule.
// x.Equal(y) when both values are non-nil. If either value is nil, Equal
// reports whether both are nil without calling the method (so a nil
// receiver never panics). Otherwise, no such method exists and evaluation
// proceeds to the next rule.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, but I don't think this is quite correct.

The difference of whether Equal is called on nil is whether it is declared on a value receiver or not. Perhaps this is more clear:

If the values have an Equal method of the form "(T) Equal(T) bool" or
"(T) Equal(I) bool" where T is assignable to I, then use the result of
x.Equal(y). The method is called even if x or y are nil and Equal
is declared on a pointer receiver. Otherwise, no such method exists and
evaluation proceeds to the next rule.

See https://go.dev/play/p/Jf2jQPmgnZx

//
// - Lastly, try to compare x and y based on their basic kinds.
// Simple kinds like booleans, integers, floats, complex numbers, strings,
Expand Down