Comments (10)
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.
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.
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.
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.
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.
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.
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.
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.
See this example: https://play.golang.org/p/hcpppV3sJEL
from go-cmp.
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)
- Diff is extremely slow HOT 3
- How to ignore specific field by name? HOT 1
- Ideas for a v2 HOT 3
- Default support for the `netip` package HOT 1
- New minor release of the library? HOT 4
- cmp.Diff() switching from property diffs to "new object" diff when object changes significantly HOT 1
- Inconsistent whitespace produced by cmp.Diff HOT 2
- cmpopts: add EquateUnsetFields(want) or FilterFieldsFrom(want) HOT 1
- IgnoreFields and nested struct HOT 2
- Enable a dependency update tool HOT 2
- Package Name Mismatches HOT 4
- unhelpful output for difference in slices HOT 1
- How to implement jests.js's `expect.any(String)` like functionality? HOT 1
- cmpopts.IgnoreFields does not work on structs with an Equal method HOT 1
- Performance Issue Comparing Array Types HOT 1
- Naming clash for new cmp standard package HOT 4
- Comparing two fields with diffirent types that are semantically equivalent HOT 3
- String diff for UUIDs can use strings.Join syntax which makes it more confusing HOT 4
- Reporter truncating important context on diff output but leaving others alone
- How to add more context to Diff output?
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 go-cmp.