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

Comments

@markusheukelom
Copy link

@markusheukelom markusheukelom commented Jul 22, 2020

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

@ianlancetaylor ianlancetaylor commented Jul 22, 2020

I believe there are similar ideas in #377.

@go101
Copy link

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

@ianlancetaylor ianlancetaylor commented Aug 11, 2020

Closing as a dup of ideas expressed in #377.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.