Skip to content
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

spec: requiring identical tags for struct identity seems overly restrictive #11661

Closed
griesemer opened this issue Jul 10, 2015 · 3 comments
Closed

Comments

@griesemer
Copy link
Contributor

The rules for struct identity require that two identical structs have identical tags.

This plays into the rules for assignments and conversions of structs in unfortunate ways:

There appears to be no way to assign or even convert a struct with tags to another struct with different or no tags, wether that other struct is named or unnamed:

http://play.golang.org/p/6FZcWSIc9w

I think we're overly restrictive here. For instance, if I wanted to convert a struct with json tags to a struct with tags for some other encoding (to convert from one encoding to another), I actually have to copy all the fields by hand.

This seems counter-intuitive. I would have expected that I could go via an unnamed struct, or at the very least use an explicit conversion.

I'm thinking that perhaps we should relax the rules here. One might be to remove again the requirement of identical tags for struct identity (simplest); or perhaps to relax the rules for assignments and conversions of structs.

@griesemer griesemer self-assigned this Jul 10, 2015
@griesemer griesemer added this to the Unplanned milestone Jul 10, 2015
@griesemer
Copy link
Contributor Author

Feedback from @ianlancetaylor (copied from e-mail):

I tend to agree. FYI, the requirement that struct tags be identical
was added here: https://codereview.appspot.com/1698043 . It seems to
me that this doesn't make much difference in practice. You already
can not assign or implicitly convert between named struct types.
Unnamed struct types are rarely used, so I don't see them as an
issue. If we reverted this CL, I think it would be possible to
explicitly convert between struct types as long as the fields had the
same names and types, because the underlying types would be identical.

@griesemer
Copy link
Contributor Author

PS: I vaguely recall that I voted for this change. But I also don't remember the details. It looks like a case of unintended consequences. Rolling back the change would be a backward-compatible change.

@robpike
Copy link
Contributor

robpike commented Jul 13, 2015

Duplicate of #6858.

@robpike robpike closed this as completed Jul 13, 2015
@golang golang locked and limited conversation to collaborators Jul 13, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants