Giter Site home page Giter Site logo

Comments (10)

flowchartsman avatar flowchartsman commented on September 16, 2024 1

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.

breml avatar breml commented on September 16, 2024

@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 suffix Of has been used/proposed for usage in the stdlib, when a function/method with the same name (without generics) already exists.
  • TypeSafeEqual or SafeEqual: might be easier to use in IDE with code completion, because already the first letter gives you the correct option

from is.

walles avatar walles commented on September 16, 2024

... or just release v2.0.0 and make the existing Equal generic.

from is.

matryer avatar matryer commented on September 16, 2024

@flowchartsman What do you think?

from is.

matryer avatar matryer commented on September 16, 2024

what if we just added a method alongside the other? Could we tolerate is.EqualG[string](s1, s2)?

from is.

flowchartsman avatar flowchartsman commented on September 16, 2024

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.

walles avatar walles commented on September 16, 2024

Related language proposal: golang/go#49085

from is.

flowchartsman avatar flowchartsman commented on September 16, 2024

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.

breml avatar breml commented on September 16, 2024

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.

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.

flowchartsman avatar flowchartsman commented on September 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.