You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
The text was updated successfully, but these errors were encountered:
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.
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.
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.
The text was updated successfully, but these errors were encountered: