proposal: Go 2: add a "scope lifting" operator ^ for short variable declarations in if and for statements #40349
Labels
FrozenDueToAge
LanguageChange
Suggested changes to the Go language
Proposal
v2
An incompatible library change
Milestone
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: