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: support struct constant #70803

Closed
1 of 4 tasks
huykingsofm opened this issue Dec 12, 2024 · 2 comments
Closed
1 of 4 tasks

proposal: spec: support struct constant #70803

huykingsofm opened this issue Dec 12, 2024 · 2 comments
Labels
LanguageChange Suggested changes to the Go language LanguageChangeReview Discussed by language change review committee Proposal
Milestone

Comments

@huykingsofm
Copy link

Go Programming Experience

Experienced

Other Languages Experience

Go, C++

Related Idea

  • Has this idea, or one like it, been proposed before?
  • Does this affect error handling?
  • Is this about generics?
  • Is this change backward compatible? Breaking the Go 1 compatibility guarantee is a large cost and requires a large benefit

Has this idea, or one like it, been proposed before?

No

Does this affect error handling?

No

Is this about generics?

No

Proposal

type RGBColor struct {
    r int
    g int
    b int
}

var (
    Red    = RGBColor{r: 255, g: 0, b: 0}   // Pure red
    Green  = RGBColor{r: 0, g: 255, b: 0}   // Pure green
    Blue   = RGBColor{r: 0, g: 0, b: 255}   // Pure blue
    Yellow = RGBColor{r: 255, g: 255, b: 0} // Pure yellow
)

Why can't the above colors be constants?

Language Spec Changes

Go should allow defining a struct constant like

const Red = RGBColor{r: 255, g: 0, b: 0}

For defining unexported structs or structs with unexported fields, Go also should support a constant function like:

constfunc NewConstRGBColor(r, g, b int) RGBColor {
    return RGBColor{r: r, g: g, b: b}
}

Informal Change

No response

Is this change backward compatible?

Yes

Orthogonality: How does this change interact or overlap with existing features?

No response

Would this change make Go easier or harder to learn, and why?

No response

Cost Description

No response

Changes to Go ToolChain

No response

Performance Costs

No response

Prototype

No response

@huykingsofm huykingsofm added LanguageChange Suggested changes to the Go language LanguageChangeReview Discussed by language change review committee Proposal labels Dec 12, 2024
@gopherbot gopherbot added this to the Proposal milestone Dec 12, 2024
@gabyhelp
Copy link

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@huykingsofm huykingsofm closed this as not planned Won't fix, can't repro, duplicate, stale Dec 12, 2024
@huykingsofm huykingsofm reopened this Dec 12, 2024
@seankhliao
Copy link
Member

Duplicate of #6386

@seankhliao seankhliao marked this as a duplicate of #6386 Dec 12, 2024
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LanguageChange Suggested changes to the Go language LanguageChangeReview Discussed by language change review committee Proposal
Projects
None yet
Development

No branches or pull requests

4 participants