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: spec: require tagged literals for external structs #2794

Closed
robpike opened this issue Jan 27, 2012 · 8 comments
Closed

proposal: spec: require tagged literals for external structs #2794

robpike opened this issue Jan 27, 2012 · 8 comments
Labels
FrozenDueToAge LanguageChange NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Proposal v2 A language change or incompatible library change
Milestone

Comments

@robpike
Copy link
Contributor

robpike commented Jan 27, 2012

It would aid compatibility and evolution if a package writer knew that all clients
creating structs with types exported from the package use the form
  pkg.Struct{A: a, B: b}
rather than
  pkg.Struct{a, b}

It may be worth making this a language restriction
@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 2:

Not before Go 2.

Labels changed: added priority-someday, removed priority-later.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 3:

Labels changed: added repo-main.

@rsc
Copy link
Contributor

rsc commented Mar 3, 2014

Comment 4:

Adding Release=None to all Priority=Someday bugs.

Labels changed: added release-none.

@ianlancetaylor
Copy link
Contributor

Comment 5:

Labels changed: added go2, removed priority-someday.

@robpike robpike added accepted v2 A language change or incompatible library change LanguageChange labels Mar 26, 2014
@robpike robpike self-assigned this Mar 26, 2014
@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@robpike robpike removed their assignment Sep 21, 2015
@rsc rsc changed the title spec: require tagged literals for external structs proposal: spec: require tagged literals for external structs Jun 17, 2017
@dsnet dsnet added the Proposal label Jun 21, 2017
@dsnet dsnet modified the milestones: Proposal, Unplanned Jun 21, 2017
@ianlancetaylor ianlancetaylor added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Dec 5, 2017
@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented Feb 5, 2019

At present there is a vet check for this:

> go vet
b.go:5:9: pkg.Struct composite literal uses unkeyed fields

Is there an advantage to promoting this check to the language level?

@griesemer
Copy link
Contributor

There might be scenarios where it's tedious to have to repeat struct field names over and over again; e.g., for small structs that are frequently created and won't change. Example

p := graphic.Point3D{1, 2, 3}

But for cases like these it's always easy (and probably better) to provide any factory function instead.

@gopherbot gopherbot removed the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Aug 16, 2019
@gopherbot gopherbot added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Sep 3, 2019
@cristaloleg
Copy link

There is a trick to force users of an exported struct to write field names:

type Struct struct {
	A int
	B string
	_ struct{}
} 

And for any usage of Struct{1, "2"} a simple compile error will appear:

./prog.go:4:16: too few values in Struct literal (Playground https://play.golang.org/p/sPgtcxAoOxd)

(source: https://groups.google.com/forum/#!topic/golang-nuts/NSjVW82i0mY)

@ianlancetaylor
Copy link
Contributor

As this is checked by vet today, it does not seem necessary to add to the language proper.

(Perhaps at some future point we should consider promoting some vet checks into the language proper.)

@golang golang locked and limited conversation to collaborators Mar 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

7 participants