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: init-only package level variables #20598

Open
iand opened this issue Jun 7, 2017 · 4 comments
Open

proposal: spec: init-only package level variables #20598

iand opened this issue Jun 7, 2017 · 4 comments
Labels
LanguageChange NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Proposal v2 A language change or incompatible library change
Milestone

Comments

@iand
Copy link
Contributor

iand commented Jun 7, 2017

Mutable state held in package-level variables can be problematic for a few reasons, e.g. concurrency [edited to remove erroneous mention of multiple imports]

It would be useful to be able to declare a form of "assign-once" variable that can only have its value set via an initializer in the declaration or in a subsequent init function. Once all init functions have completed the value of the variable cannot be changed.

Some other languages use a special keyword to indicate this type of variable (e.g. pony uses let for assign-once variables and var for others)

@ianlancetaylor ianlancetaylor changed the title Init-only package level variables proposal: init-only package level variables Jun 7, 2017
@gopherbot gopherbot added this to the Proposal milestone Jun 7, 2017
@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change Proposal and removed Proposal labels Jun 7, 2017
@ianlancetaylor
Copy link
Contributor

This is a language change so marking as a proposal for Go 2.

@griesemer
Copy link
Contributor

@iand This is somewhat vague a proposal. Can you elaborate a bit? For instance, how "deep" is the "freezing" of such assign-once variables? If I have a pointer or slice in an assign-once variable, can I still change the contents of the value pointed to or the elements of the slice?

@josharian
Copy link
Contributor

I can imagine how to implement this for composite liberals, but it is harder for init functions—many creative ways to get bits of the structure to escape, particularly if it is a compile time check.

One nice thing about this for maps is that it would let the compiler substitute data structures optimized for lookups, which could provide very substantial speed-ups.

@andlabs
Copy link
Contributor

andlabs commented Jun 7, 2017

"repeated imports of a single package" — aren't packages only imported and initialized once in an output binary, thanks to the guarantee of no import cycles? Do c-archives/c-shareds/plugins each have their own separate copies of packages?

@rsc rsc changed the title proposal: init-only package level variables proposal: spec: init-only package level variables Jun 16, 2017
@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LanguageChange NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

7 participants