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

Improve name scope in typescript for multi-initialization statements #1742

Open
pokey opened this issue Aug 7, 2023 · 5 comments
Open

Improve name scope in typescript for multi-initialization statements #1742

pokey opened this issue Aug 7, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@pokey
Copy link
Member

pokey commented Aug 7, 2023

The problem

In code like the following:

const aaa = 0, bbb = 1;

we could improve our support for the name scope.

The solution

We should probably handle the case of a simple initializer with only one variable separately from a multi-child one

Single-variable initializer

For example:

const aaa = 0;

In this case, we want the following:

  • A single scope whose domain is the entire statement

This way you could say:

  • "name" from within the statement to get the name
  • "every name" from anywhere in the block containing the statement (including within statement itself) to get all the names in that block

Multi-variable initializer

For example:

const aaa = 0, bbb = 1;

In this case, we want the following:

  • one scope per nested assignment (aaa = 0 and bbb = 1)
  • a parent scope whose domain is the entire statement, and has multiple content ranges, one per sub-assignment name
  • an iteration scope that is the entire statement

This way you could say:

  • "name" from within a sub-assignment to get the name of that sub-assignment
  • "name" from the start / end of the statement to get all the names in that statement
  • "every name" from anywhere in the statement to get all the names in that statement
  • "first name" from anywhere in the statement to get the first name in that statement
  • "every name" from anywhere in the block containing the statement, except within statement itself, to get all the names in that block

Discussion

Note that we may want to use this pattern in other places

  • Talon condition blocks
  • Go return values
  • Tags in .scm files
@pokey pokey added the enhancement New feature or request label Aug 7, 2023
@auscompgeek
Copy link
Member

Would you also want the same sort of thing in Python tuple unpacking?

@pokey
Copy link
Member Author

pokey commented Aug 12, 2023

Good question. My instinct is to say no. For that it's generally more useful to just target the entire left-hand side. But I think @josharian wants something like that for Go, so maybe we should discuss

@josharian
Copy link
Collaborator

Yeah, in Go, code like this is very common:

x, err := foo()

and wanting to refer to just x or err individually is also going to be very common.

(It's not tuple unpacking, it's multiple assignment, but it's very similar.)

@pokey
Copy link
Member Author

pokey commented Aug 12, 2023

I guess you'd want to be able to say eg "chuck first name", "pour last name", etc?

@josharian
Copy link
Collaborator

Yeah, after a refactor that changes what a function returns, it's going to be very common to want to chuck or pour or clone or swap individual names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants