-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: reform the variable redeclaration syntax to avoid some confusions and inconveniences #38388
Comments
Aha, I suddenly realized that
(Edit: is |
For language change proposals, please fill out the template at https://go.googlesource.com/proposal/+/refs/heads/master/go2-language-changes.md . When you are done, please reply to the issue with Thanks! |
Er, I tried to search this template before posting the proposal, but I only found this one instead. OK, the following uses the new template form: ================================================================ Would you consider yourself a novice, intermediate, or experienced Go programmer?Experienced (at least in grammar, ;D) What other languages do you have experience with?C, C++, AS3 Would this change make Go easier or harder to learn, and why?Easier. At least less confusions for new gophers, and it removes some inconveniences in Go programming. Has this idea, or one like it, been proposed before?There are some similar ones, but none of them proposes to retire short variable declarations
This proposal also reuses the the labels concepts as scope viewers. Here is an example: func f() {
var a = 1
x:
{
y:
var a = 2
z:
{
var a = 3
// x'a <=> y'a
println(x'a, y'a, z'a, a) // 1 1 2 3
}
}
} Before, it would be written as the following (less readable in my opinion): func f() {
var a = 1
var p1 = &a
{
var a = 2
var p2 = &a
{
var a = 3
// x'a <=> y'a
println(*p1, *p1, *p2, a) // 1 1 2 3
}
}
} Who does this proposal help, and why?It removes some confusions for new gophers, and it removes some inconveniences in Go programming. What is the proposed change?The proposal proposes to retire short variable declarations If the Precisely, it proposes the following line
can be expressed as a less confused way:
And it recommends the following code
should be written as (already supported now):
for better readability. The third, for continence, it also proposes that legal L-values in general assignments
Please note, the scope viewer concept is an optional feature in this proposal. If it is not ignored, then there is an alternative form for
Is this change backward compatible?Yes (for what I can tell now). Here what I mean is this proposal doesn't require to retire short variable declarations immediately. Short variable declarations may be retired eventually. Show example code before and after the change.An example (in a survey, 21 of 35 participants think the following program prints 6,
The correct version (some verbose):
After the change (It is less confused and still avoids verboseness):
What is the cost of this proposal? (Every language change has a cost).How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected?Many, but the impact is expected to be smaller than introducing type aliases, and it is easy to update these tools. What is the compile time cost?Very small. What is the run time cost?None. How would the language spec change?Add a new variable declaration+assignment hybrid statement.
Orthogonality: how does this change interact or overlap with existing features?It avoids one variable declaration form. The scope viewer concept is good in orthogonality with labels. Is the goal of this change a performance improvement?No. Does this affect error handling?No. Is this about generics?Not. |
@gopherbot please remove label WaitingForInfo. |
The proposal described in the template (#38388 (comment)) seems significantly different from the proposal described in the original post on this issue. For example, there are no scope viewers in the template discussion. What is the actual proposal here? Thanks. The idea of annotating the variables that should be newly declared in a |
There is a problem in the form
This is half true. None of the ideas in #377 propose to retire the current re-declaration syntax form. |
Removing a widely used feature like redeclarations on
|
The
form can coexist with the current redeclarations.
Yes, this is true. It has happened for "named type" definition and the behavior of fmt.Print(aMap). It is really hard to totally avoid such situations. |
Sorry, we're still not clear on what the actual proposal is here. It's hard to track the changes in this issue. In
is Are scoped variable references still required by this proposal? Can this proposal be described in just a few succinct lines? Thanks. |
@ianlancetaylor Here, I summarize the proposal simply. The proposal suggests three changes to Go. 1.Recommend to replace the current re-declaration syntax form
with a clearer form.
The new clearer form shows which identifiers are re-declared clearly. 2.Support "variable declaration + assignment" hybrid statement, as long as there is at least one pure identifier target item. For example,
3.Use labels as scope viewers. An example:
Some ideas in this proposal have been mentioned in other issue threads, but this one differs from them by recommending to retire the current re-declaration syntax form. |
I'm sorry, I just mis-wrote
as
|
Thanks. Your change 1 and 2 from #38388 (comment) seem to be adequately covered by #377. I'm not clear on when change 3 would be useful. If you want to refer to a variable in an outer scope, don't shadow it. It seems to me that it is rather confusing to shadow a variable from an outer scope if you then need to refer to it. Introducing a way to get around that shadowing seems like adding complexity to avoid a complex situation, when there is already an easy way to simplify the situation instead by renaming the variable. |
As I mentioned above, though, we really can't remove the current re-declaration syntax. So the best we could do to "retire" it would be to advise people writing new code to use a different form. |
Sorry for my bad description. In fact, this is exactly the way that this current proposal suggests. |
As discussed above, parts of this are covered by #377. The idea of referencing variables in different labeled scopes is new, but does not seem like a feature that is essential to the language. People can instead rename variables. Also, emoji voting for this proposal is negative. For those reasons, this proposal is itself a likely decline. We can pursue further discussions on #377. Leaving open for four weeks for final comments. |
The other two issues don't pursue retiring the |
No change in consensus. Closing. |
(The following content is a rewritten and an improvement version of this comment)
Abstract
This proposal tries to avoid some problems caused by short variable declarations (a.k.a., variable redeclarations), by introducing a scope viewer identifier concept and using labels as scope viewer identifiers.
Background
We all know that, although short variable declarations (
x, y := a, b
) do solve some problems, they also brings some new ones. The new problems include:Proposal
Proposal part 1: scope viewers
The proposal proposes that we can re-use labels as scope viewer identifiers. The following code shows what are scope viewer identifiers:
Proposal part 2: syntax sugars
There are two sugars for the
scope:identifier
notation.:identifier
(without the scope prefix), means the innermost declaredidentifier
(it is equivalent to*&identifier
).::identifier
, means the package-level declaredidentifier
.(Digression 1: is it good to view package import names as scope viewer identifiers? So that we can use
aPkg::ExportIdentifier
to use exported resources.)(Digression 2: the proposal may also discard the above described scope viewer concept and only contain the following content, by replacing
:identifier
with*&identifier
and using*&identifier
as redeclared items.)Proposal part 3: avoid using short declarations by using scope viewer identifiers
The proposal proposes that we can relax the restriction of the left items in standard variable declarations. Now all left items must be pure identifiers. We can relax the rules as selectors, dereferences, element indexing, and the above mentioned
scope:identifier
forms are allowed to show up as target values in the left items of a standard variable declaration, as long as there is at least one pure identifier in the left items as well. When the declaration is executed at run time, the new variables represented by pure identifiers are initialized, others are re-assigned with new values. The following is an example without using labels:Another more common use case:
The
:=
redeclaration syntax should be kept ONLY in the simple statements in control flows, just for aesthetics reason. In other words,... := ...
is totally a shorthand ofvar ... = ...
in the simple statements in control flows.Rationale
Please see the above backgroud part.
Compatibility
The idea should be Go 1 compatible. What I mean here is the above proposed new redeclaration syntax may coexist with the current standard and short variable declarations.
It is easy to let
go fix
translate short variable declarations into the above proposed new syntax form. The short declarations in the simple statements in control flows don't need to be translated.If it needs, the current short declaration uses can be removed eventually.
Implementation
Compiler parsers should consider the new syntax form.
One new statement might need to be added to
go/ast
standard package.The text was updated successfully, but these errors were encountered: