You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Operator ^ lifts the declaration of a variable to the enclosing scope when used in if and for statements:
if ^f, err := os.Open("foo.bar"); err != nil {
return fmt.Errorf("error opening file: %w", err)
}
f.Read() // ok: f is in scope now
Using ^f, the declaration of f escapes the if statement while keeping err local to the if statement.
The expression ^f, err := works just like f, 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:
@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:
So I don't really understand the example. Both lines do not seem to be valid go?
martisch
changed the title
[proposal] Add a "scope lifting" operator ^ for short variable declarations in if and for statements
proposal: Add a "scope lifting" operator ^ for short variable declarations in if and for statements
Jul 22, 2020
ianlancetaylor
changed the title
proposal: Add a "scope lifting" operator ^ for short variable declarations in if and for statements
proposal: Go 2: add a "scope lifting" operator ^ for short variable declarations in if and for statements
Jul 22, 2020
The other two similar ideas are using (id) and *&id to indicate an identifier that it has been declared in outer scopes.
The two notations don't introduce new notations, they are already supported now, though they are both a little more verbose.
Ref: #38388, #30318, #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: