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

reflect: when creating anonymous fields, StructOf should attempt to compute the field name #26035

Closed
jimmyfrasche opened this issue Jun 24, 2018 · 5 comments
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@jimmyfrasche
Copy link
Member

Creating an anonymous field with reflect.StructOf panics if not explicitly given a name.

A name must be provided when the embedded type is not defined or the name differs from the name of the defined type, to simulate embedding of an alias to a defined type. It is necessary to require an explicit name in these cases.

However, the common case, and hence user expectation, is that embedding a defined type creates a field with the same name as the defined type.

For any T representing a defined type, the following should be equivalent:

reflect.StructOf([]reflect.StructField{
  {
    Name: T.Name(), // Or T.Elem().Name()
    Type: T,
    Anonymous: true,
  },
})

and

reflect.StructOf([]reflect.StructField{
  {
    Type: T,
    Anonymous: true,
  },
})

Originally reported by @Merovius as part of #24781. I'm breaking it into a separate issue for easier tracking.

@gopherbot
Copy link

Change https://golang.org/cl/120698 mentions this issue: reflect: StructOf compute anonymous field name

@jimmyfrasche
Copy link
Member Author

cc @rsc @ianlancetaylor per golang/s/owners

@ianlancetaylor ianlancetaylor added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Jun 25, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.12 milestone Jun 25, 2018
@ianlancetaylor
Copy link
Contributor

Probably a dup of #24781.

@jimmyfrasche
Copy link
Member Author

@ianlancetaylor I forked this off that issue because it has 2 or 3 other things going on that may or may not be bugs (which may be a dupe of #24721 though this is not a dupe of that). This can be handled and tracked separately.

@rsc
Copy link
Contributor

rsc commented Sep 26, 2018

Given how difficult and subtle this all is, it seems better to require the caller to specify the name, as we always have. I'm already skeptical of StructOf, but more skeptical of more magic. Also we learned during alias that we may need to have the idea of anonymous fields with names different from types. The current behavior is more explicit, which is good.

@rsc rsc closed this as completed Sep 26, 2018
@golang golang locked and limited conversation to collaborators Sep 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

4 participants