Skip to content
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

Functions are handled differently from reflect.DeepEqual #46

Closed
countcb opened this issue Jan 29, 2021 · 3 comments
Closed

Functions are handled differently from reflect.DeepEqual #46

countcb opened this issue Jan 29, 2021 · 3 comments

Comments

@countcb
Copy link

countcb commented Jan 29, 2021

deep.Equal does behave differently from reflect.DeepEqual when comparing functions.

The documentation of reflect.DeepEqual states for functions:

Func values are deeply equal if both are nil; otherwise they are not deeply equal.

Example Code:

func testFunc(i int) int {
	return i
}
func TestEqualForFunctions() {
	type TestStruct struct {
		Function func(int) int
	}
	t1 := TestStruct{
		Function: testFunc,
	}
	t2 := TestStruct{
		Function: testFunc,
	}
	fmt.Println("reflect", reflect.DeepEqual(t1, t2))
	fmt.Println("deep", deep.Equal(t1, t2))

	t1.Function = nil
	fmt.Println("reflect", reflect.DeepEqual(t1, t2))
	fmt.Println("deep", deep.Equal(t1, t2))

	t1.Function = nil
	t2.Function = nil
	fmt.Println("reflect", reflect.DeepEqual(t1, t2))
	fmt.Println("deep", deep.Equal(t1, t2))
}

Output:

reflect false 
deep []  // should state that both functions are != nil
reflect false
deep [] // should state that function 1 is not nil but function 2 is nil
reflect true
deep []
@countcb
Copy link
Author

countcb commented Jan 29, 2021

If this gets added/changed then maybe it would be a good idea to add a field
deep.CompareFunctions

similar to deep.CompareUnexportedFields

@daniel-nichter
Copy link
Member

Fixed in #55. Will be released soon.

@daniel-nichter
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants