GithubHelp home page GithubHelp logo

Comments (10)

dsnet avatar dsnet commented on August 19, 2024

Hi, I'm not sure how I can get that to work though. When the signature of the input changes from func(a, b T) bool to func(a, b interface{}) bool, I no longer know what which slice types to apply the transformation to. Currently, the implementation looks at the less function to determine that it is supposed to sort []T as opposed to []R.

from go-cmp.

dsnet avatar dsnet commented on August 19, 2024

With generics seriously being considered for Go2, I think it be best to just leave the API as is and see how can we make it better. There are other cases where generics would have been very helpful.

from go-cmp.

Goodwine avatar Goodwine commented on August 19, 2024

The problem I have with SortSlices is that it takes a "less" function typed as interface{}, I immediately think of sort.Slice() which has the param type func(i, j int) bool, but no, this one takes func (a, b T) bool which is currently impossible to represent in Go.

I had to read a couple times the documentation on how to get this to work, and sure it does, however I think saying the func takes a param type func(a, b interface{}) bool and say that a and b have to be type asserted by the user feels like a better API.

I agree this would be nicely solved by Go2, but it sounds like that's probably happening until 2 or 3 more years.

from go-cmp.

dsnet avatar dsnet commented on August 19, 2024

You didn't really address the problem I posed earlier about how func(a ,b interface{}) removes information needed to properly implement the function.

from go-cmp.

Goodwine avatar Goodwine commented on August 19, 2024

I already mentioned that in "My desired state of the world" and on the previous comment that the user would be responsible for doing type assertion in this world without generics.

e.g.

func (a, b interface{}) bool { a.(Foo).Val < b.(Foo).Val }

from go-cmp.

dsnet avatar dsnet commented on August 19, 2024

A desired state of the world isn't helpful if it is not implementable. As it stands, I don't think there is anything actionable here. The proposed API can't work and the other alternative is to wait for generics.

from go-cmp.

Goodwine avatar Goodwine commented on August 19, 2024

What do you mean by not implementable? Should I send a PR? Maybe I'm not explaining myself properly.

Right now the API accepts an interface{} that says nothing, I can pass a string and it will compile. I know that it will fail at runtime but it's not obvious. My proposal is that for now we change the signature of SortSlices(interface{}) to SortSlices(func(a,b interface{})bool) until generics exist.

Could you clarify why is having no types better than having some types and passing the responsibility of type-asserting to the user?

from go-cmp.

dsnet avatar dsnet commented on August 19, 2024

Right now the API accepts an interface{} that says nothing

interface{} says nothing to the compiler, but still says something at runtime. Reflection tells me what the type of the arguments are.

func(interface{}, interface{}) bool says a little more to the compiler, but says nothing at runtime when performing type introspection.

from go-cmp.

dsnet avatar dsnet commented on August 19, 2024

See this example: https://play.golang.org/p/hcpppV3sJEL

from go-cmp.

Goodwine avatar Goodwine commented on August 19, 2024

Ah... so IIUC SortSlices is not intended to be applied only to A and B in cmp.Diff(A, B), it's intended to sort any slice because if I pass a map or a struct, then each entry/property is navigated like a tree and when a slice is found if it matches the type of my less function, then the function is applied to it.

I think that's what you tried to point out on your first reply, but it wasn't that obvious. I thought SortSlices was used iff I was comparing 2 slices. Now that I get it I agree with you and waiting for generics is the best thing to do.

Thanks for the discussion 😃

from go-cmp.

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.