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: Go 2: add a "scope lifting" operator ^ for short variable declarations in if and for statements #40349

Closed
markusheukelom opened this issue Jul 22, 2020 · 5 comments
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@markusheukelom
Copy link

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:

  1. ^ 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

  2. Single lifts are allowed only; ^^f is illegal, etc.

  3. 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

  4. I chose ^ because it is a visual cue for the effect of the operation.

@D1CED
Copy link

D1CED commented Jul 22, 2020

^ is already an unary operator (bitwise complement; same as ~ in C)

Edit: Example

if *a /* dereference */ := *b /* dereference */; true { ... }
if ^a /* scope lift */ := ^b /* complement */; true { ... }

Edit2: Well the first line of the example would not work because a can not be a new variable but you get my point.

@markusheukelom
Copy link
Author

markusheukelom commented Jul 22, 2020

@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?

@martisch 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
@martisch martisch added v2 A language change or incompatible library change LanguageChange Proposal labels Jul 22, 2020
@gopherbot gopherbot added this to the Proposal milestone Jul 22, 2020
@ianlancetaylor 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
@ianlancetaylor
Copy link
Contributor

I believe there are similar ideas in #377.

@go101
Copy link

go101 commented Jul 24, 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

@ianlancetaylor
Copy link
Contributor

Closing as a dup of ideas expressed in #377.

@golang golang locked and limited conversation to collaborators Aug 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

6 participants