-
-
Notifications
You must be signed in to change notification settings - Fork 58
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
Proposal: Use generics to add type safety support to is.Equal #52
Comments
@leafbebop in general, I like this proposal. I think, this could be a reasonable addition to the existing API. For the name of this function, I would like to share the following proposals:
|
... or just release v2.0.0 and make the existing |
@flowchartsman What do you think? |
This won't work as-is. Go generics do not support parameterized methods, so the above will not compile. You could try to write a package global method with a generic signature like var (
a int
b uint8
)
isVal := is.New(t)
isVal.True(is.EqualGeneric(a,b))
// error: type uint8 of b does not match inferred type int for T And that error message is also not particularly clear, since it's primarily a type inference error regarding the generic function itself, which is confusing since there's no var (
a int
b uint8
)
isVal := is.New(t)
isVal.True(is.EqualGeneric[int](a,b))
// output: cannot use b (variable of type uint8) as int value in argument to is.EqualGeneric[int] Ref: https://go.dev/play/p/tKb-6wvHZbx Given all that, I'm not sure using type parameters really gets us what we want here. |
what if we just added a method alongside the other? Could we tolerate |
It still can't be a method, with or without type inference. You'd need to make it a global, type-parameterized function in the |
Related language proposal: golang/go#49085 |
Yah, I wouldn't hold my breath for that one, much as I would love it, since it seems to be relatively difficult to solve in a go-y way. At this time, AFAICT, the only way to do this in a way that looks how the package is used now is to go all-in on global state, and have package-level parameterized functions in place of methods on instances of the |
Even with all the sympathy I have with the original idea of this proposal, I feel that changing the API of the |
Given that this proposal can't work as-is (no generic methods), and given that fully embracing generics would be a big enough change to merit an entirely new version (if not a new module), I think we're gonna have to close this issue, but thanks for the submission, and stay tuned! |
I think it is quite common to got caught into pitfalls getting errors like "unit8(0) != int(0)" (by calling is.Equal with an untyped constant for example).
Now that Go has added generics support, it is easy to add type safety support to equal, like:
The text was updated successfully, but these errors were encountered: