Comments (10)
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
func EqualGeneric[T any](a,b T) bool
, but you would then have to call it using the package, which would mean abandoning the convention of is := is.New()
, since this otherwise shadows the package namespace with the identifier. This gets you something 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 T
in this code. It is more clear with an explicit type parameter to the function, but then you end up with something like:
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.
from is.
@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:
EqualOf
: if I remember correctly, the suffixOf
has been used/proposed for usage in the stdlib, when a function/method with the same name (without generics) already exists.TypeSafeEqual
orSafeEqual
: might be easier to use in IDE with code completion, because already the first letter gives you the correct option
from is.
... or just release v2.0.0 and make the existing Equal
generic.
from is.
@flowchartsman What do you think?
from is.
what if we just added a method alongside the other? Could we tolerate is.EqualG[string](s1, s2)
?
from is.
what if we just added a method alongside the other? Could we tolerate
is.EqualG[string](s1, s2)
?
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 is
package, breaking the naming convention like I mentioned above, or make the I
type itself generic, and instantiate a new one for every type you wanted to compare (which, would also mean never calling the I
value is
, and would also mean you couldn't re-use it, since they'd be different types after the short assignment).
from is.
Related language proposal: golang/go#49085
from is.
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 I
type, but this is a very radical change to make just to get the compiler to tell you that one thing is an int
and another is a uint8
.
from is.
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
I
type, but this is a very radical change to make just to get the compiler to tell you that one thing is anint
and another is auint8
.
Even with all the sympathy I have with the original idea of this proposal, I feel that changing the API of the is
package in such a dramatic way is not reasonable. This would justify the creation of a completely separate package with its own API style.
from is.
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!
from is.
Related Issues (20)
- Additional error assertations HOT 8
- flag.Parse in init() prevents own flags in tests HOT 7
- -nocolor flag not supported by Go Test in go-1.11.4 HOT 4
- Proposal: show var names in output HOT 8
- Lines should be escaped before printing
- Add a Contains method HOT 3
- My editor thinks this is bad practice HOT 4
- New Release? HOT 1
- Proposal: is.Less and is.Greater HOT 2
- Ditch the indentation? HOT 1
- Small simplification
- Potential issue when running go test -json HOT 4
- Is the project still alive? HOT 5
- Proposal: value matcher interface in is.Equal HOT 2
- Release v1.5.0 HOT 2
- Revisiting `is.Equal` and the role of `reflect.DeepEqual` HOT 11
- gofmt all the things
- Issue equating zero `big.{Int,Float}`'s HOT 2
- Proposal: custom stacktrace HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from is.