Skip to content

proposal: reflect: DeepEqual to consider identical function pointers as equal #71311

@iwahbe

Description

@iwahbe

Proposal Details

Currently, reflect.DeepEqual considers all non-nil functions as un-equal, even if they are the same function:

go/src/reflect/deepequal.go

Lines 154 to 159 in 1a93e4a

case Func:
if v1.IsNil() && v2.IsNil() {
return true
}
// Can't do better than this:
return false

reflect.DeepEqual should be extended to consider identical functions as equal:

 case Func: 
 	return v1.Pointer() == v2.Pointer()

This would allow users to assert that one struct "is" another struct:

func TestRoundTrip(x T) {
 	if !reflect.DeepEqual(x, blackBox(x)) {
 	 	panic("not equal")
 	}
}

That identical functions are not equal is surprising.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions