-
Notifications
You must be signed in to change notification settings - Fork 18.1k
proposal: Go 2: add a "scope lifting" operator ^ for short variable declarations in if and for statements #40349
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
Comments
Edit: Example
Edit2: Well the first line of the example would not work because a can not be a new variable but you get my point. |
@D1CED thanks for the example. According to language specification no expressions are allowed currently allowed on the left hand side of a short variable declaration: https://golang.org/ref/spec#Short_variable_declarations So I don't really understand the example. Both lines do not seem to be valid go? |
I believe there are similar ideas in #377. |
Closing as a dup of ideas expressed in #377. |
Operator
^
lifts the declaration of a variable to the enclosing scope when used in if and for statements:Using
^f
, the declaration of f escapes the if statement while keeping err local to the if statement.The expression
^f, err :=
works just likef, err :=
except that if f is declared, it is declared in the enclosing block.Other notes:
^ can only be used with if and for; anywhere else it is illegal
To be more exact:
- in the ShortVarDecl of the SimpleStatement of the IfStmt
- in the InitStmt of the ForClause of the ForStmt
- in the RangeClause of the ForStmt
Single lifts are allowed only; ^^f is illegal, etc.
I know that ^ is used as bitwise XOR operator; this should be no problem as for example * is also used for both derefencing and multiplication operator
I chose ^ because it is a visual cue for the effect of the operation.
The text was updated successfully, but these errors were encountered: