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

slices: make slice equality comparator flexible on target type #100

Merged
merged 1 commit into from
Jan 21, 2023

Conversation

shoenig
Copy link
Owner

@shoenig shoenig commented Jan 21, 2023

This PR enables SliceContainsFunc to accept a different type as the
target from the slice elements type. For example if we have a slice
of Person objects but want to detect if the slice contains a person
with a particular name (string), we can now use a comparator of
signature func(Person, string) bool - rather than needing a whole
Person struct for the target.

Closes #88

Worth noting that technically just the predicate function is sufficient, as is done in the slices package https://pkg.go.dev/golang.org/x/exp/slices#ContainsFunc - however in the context of test case assertions I think it's more valuable to have the target value explicitly part of the assertion signature.

This PR enables SliceContainsFunc to accept a different type as the
target from the slice elements type. For example if we have a slice
of Person objects but want to detect if the slice contains a person
with a particular name (string), we can now use a comparator of
signature func(Person, string) bool - rather than needing a whole
Person struct for the target.
@shoenig shoenig changed the title slices: make slice equality comparator more flexible slices: make slice equality comparator flexible on target type Jan 21, 2023
@shoenig shoenig merged commit 7d8bca6 into main Jan 21, 2023
@shoenig shoenig deleted the flexible-slice-contains-func branch December 15, 2023 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: SliceContainsFunc should be more flexible on types
1 participant