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
x/exp/maps: Copy should use two generic map types #52593
Comments
I disagree with this proposal. IMHO there is a semantical difference between When comparing two maps for equality, With This way if you have some specific code where the types don't matter, you would use an explicit conversion:
Note that in real code the type declarations may be far away from the point of usage, so forcing the explicit conversion makes for clearer code. |
I prefer to think of it more practically: What do we gain as writers and readers of code? Is it more clear as a reader if there's an explicit conversion? It doesn't seem so. If you see Does a writer gain any safety? Could it prevent a mistake where I have three maps, two of the same type and one of a second and I accidentally put in the wrong variable? It could - though it seems unlikely that this case is going to arise frequently enough to make decisions based on that possibility alone. Furthermore it may not be possible to access the map types to perform the conversion since they would have to be exported, and then you've no recourse remaining other than copy-pasting the implementation of maps.Copy - though not having have access to both map types is equally unlikely as juggling 3 maps with only 2 being of compatible type, but the consequences are certainly grating if it occurs. I see this as a simple improvement in ergonomics. If I'm calling |
Since |
We added the M type parameter to match the many other functions in the package. |
This proposal has been added to the active column of the proposals project |
No parameter at all would make it inconsistent with other functions signatures, and actually lose flexibility in the (rather uncommon) case of declaring variables of function type.
If a change must be made, it's better to have |
Ian points out there are other functions with two map parameters, like Equal, so I take back my suggestion to have zero. |
Code like
will not work with two parameters. You will have to write:
This seems somewhat awkward. On the other hand, if you have one parameter, this fails:
That doesn't bother me a lot because this doesn't type-check either:
Of course if you really want to force it, you can do:
or
On the other hand, Equal already takes two. (In fact Copy is the only function that takes two maps as arguments but only has one type parameter.) So probably Copy should match. |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
Change https://go.dev/cl/408894 mentions this issue: |
There is an argument for having the
Copy
function in thex/exp/maps
package take two generic map types.The reason for this is the same reason behind
Equal
andEqualFunc
allowing two disparate map types. A programmer might decide that the semantics of the two types do not matter and they still wish to compare or copy/merge them.Without the second generic map type, the following fails:
Presumably the decision was made for
Equal
andEqualFunc
for the same reasons that the decision to change this would be made. I could not find the reasoning behind the decision to have two map types for the equality functions in the first place but that would provide guidance on creation of future APIs similar to Equal/Copy.As it is, it seems to make sense to either make
Equal
andEqualFunc
take a single generic map type, or to makeCopy
take a second.The text was updated successfully, but these errors were encountered: