-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: Go 2: define slice equality #23725
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
Comments
Related to #21829 |
Given the use case of maps, this is also related to #395. |
Personally I find the introduction of a unique special case for the handling of slice values as map keys to be rather troubling. |
Note, that the type is not self-referencing, but the value is. And note, that this isn't a problem with arrays, as they would get copied, whereas slices share the same backing array. |
This seems problematic to me. It means that an expression like
I don't believe it's possible to statically determine if a type is potentially self-referencing, unless you prohibit all slice comparisons that are made through an interface, because interface members can always be self-referential. Consider this code, for example:
|
It seems non-orthogonal to say that slices used as a map key are copied. It seems disastrous to not copy them. As was stated above, some people prefer a different equality mechanism, so picking this one makes the language more confusing for some people. |
Slices should have equality defined which would allow slice types to be used as map keys and in comparison with == and !=.
An implementation proposal is that slices are compared by nil versus non-nil, length and backing array pointer, and then by the rules for array if length is equal but different arrays are pointed.
Additionally self-referencing types using a slice cannot be compared, and values of map key types using a slice are copied; map types with a slice key can only be keyed by slice value.
In my experience this would have been useful for a map keyed by a path of X,Y coordinates. With Go 1 in this case a pointer to a slice was used, which means consumers of the map can do comparisons between these paths but cannot access the map using new paths without possible duplicates.
The choice of comparison by slice header vs by slice value is arbitrary and debated:
The topic is discussed here: https://groups.google.com/forum/#!topic/golang-nuts/ajXzEM6lqJI
In the first part of this golang-nuts thread one person asks for by-header while four ask for by-value. I’m in the by-value camp and I think picking one is better than having neither.
The text was updated successfully, but these errors were encountered: