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

proposal: Go 2: improve the initialization of embedded anonymous struct #29438

Closed
shiyanhui opened this issue Dec 28, 2018 · 7 comments
Closed
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@shiyanhui
Copy link

shiyanhui commented Dec 28, 2018

Problem

Now it's very redundant to initialize an embedded anonymous struct. We have to re-define the same anonymous struct in the assigned value. For example,

user := struct {
	name struct{
		first string
		last string
	}
	home string
}{
	name: struct{
		first string
		last string
	}{
		first: "Bob",
		last: "LastName",
	},
	home: "Earth",
}

We have to re-define the same anonymous struct struct {first string; last string} again in the value which is unnecessary cause we have already known the definition of the anonymous struct type.

Solution

Remove the unnecessary re-defined anonymous struct in the value. For example,

user := struct {
	name struct{
		first string
		last string
	}
	home string
}{
	name: {
		first: "Bob",
		last: "LastName",
	},
	home: "Earth",
}

or

user := struct {
	name struct{
		first string
		last string
	}
	home string
}{{"Bob", "LastName"}, "Earth"}
@shiyanhui shiyanhui changed the title improve the initialization of embedded anonymous struct proposal: improve the initialization of embedded anonymous struct Dec 28, 2018
@gopherbot gopherbot added this to the Proposal milestone Dec 28, 2018
@ianlancetaylor ianlancetaylor changed the title proposal: improve the initialization of embedded anonymous struct proposal: Go 2: improve the initialization of embedded anonymous struct Dec 28, 2018
@ianlancetaylor ianlancetaylor added LanguageChange v2 A language change or incompatible library change labels Dec 28, 2018
@ianlancetaylor
Copy link
Contributor

I think this is a subset of #12854.

@shiyanhui
Copy link
Author

shiyanhui commented Dec 28, 2018

Yeah. And I think the main difference is:

  • proposal: spec: type inferred composite literals #12854 is a big change for the language and is more ambiguity and less readable.
  • Embedded anonymous structs are mostly used for once and will unlikely to be exposed. For example, the test cases in xx_test.go. It's not worth to define the struct which we used just once. Mostly we can find the definition of the anonymous struct in the same function, so this solution doesn't hurt the readability and at the same time can bring us more convenience.

@bitfield
Copy link

I'm struggling to see the value in this. If you use the same anonymous struct twice inside another struct, it's not a big deal just to duplicate it. If it is a big deal, then why not define it as a named struct instead? It seems odd to say "this struct is so onerous to repeat that I need to name it, but only within the scope of this other struct it's embedded in".

@teejays
Copy link

teejays commented Jan 6, 2019

@bitfield I think it can be an issue in systems that deal with deeply nested structs (e.g. nested JSON API responses). In those cases, it's not always friendly to declare each nested struct as a named struct.

@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented Jan 15, 2019

Let's merge this into #12854. The ideas are very similar.

@networkimprov
Copy link

@ianlancetaylor I think you meant #12854 :-)

@ianlancetaylor
Copy link
Contributor

@networkimprov Yes, thanks. Updated comment.

@golang golang locked and limited conversation to collaborators Jan 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

6 participants