-
Notifications
You must be signed in to change notification settings - Fork 50
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
Generics in comparison functions? #249
Comments
Hey Glen! I think it would make sense to start using generics for I'm hoping it'll be a backwards compatible change. Any assertion that uses different types for I guess the signatures would look like this: func Equal[X any](t TestingT, x X, y X, msgAndArgs ...any) { ... }
func DeepEqual[X any](t TestingT, x X, y X, opts ...gocmp.Option) { ... } I think it's only the exported function signatures that need to change so that the compiler can check the types match. Otherwise I think all the internals probably don't need to change. What do you think? Are there other functions that might benefit from generics? Do you know of any potential problems with changing A little while ago I added a property check that used generics here: https://github.com/gotestyourself/gotest.tools/blob/main/x/generics/property/complete.go#L60-L73, I couldn't quite get the module alias working, so I don't think it's importable yet. Once the main package is using generics I think I'll move that to |
That's great! This pretty much aligns with what I was thinking - I think possibly it'd be nice if I don't know whether it's worth adding something like The case I ran into recently that prompted this was incorrectly comparing a struct with a pointer to the same struct type - which isn't particularly a massive pain as the diff does include the (rather subtle) Given the relative stability of this package it's probably not worth using build tags to continue to provide backwards compatibility for projects using older Go versions that want to continue to update |
Nice! I like the idea of type-safe implementations like |
I put together #255 which adds generics to the main assertions, if you'd like to check it out. For // strings
assert.Assert(t, strings.Contains(actual, expected), actual)
// slices or maps
assert.DeepEqual(t, actual, expected, cmpSomething)
// slice or map empty
assert.Assert(t, len(actual), 0, "got: %v", actual) For slices and maps, I always prefer |
Hello!
I was wondering if you'd considered whether it's worth it to use generics in the comparison functions, and what a migration path to that model might look like?
The text was updated successfully, but these errors were encountered: