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

tuple destructuring doesn't always work with _ (underscore) #18710

Closed
a-mr opened this issue Aug 18, 2021 · 5 comments · Fixed by #22537
Closed

tuple destructuring doesn't always work with _ (underscore) #18710

a-mr opened this issue Aug 18, 2021 · 5 comments · Fixed by #22537

Comments

@a-mr
Copy link
Contributor

a-mr commented Aug 18, 2021

Tuple desctructuring does not work with underscores if used outside of var section. Inside it it's OK, and that's confusing. It seems Nim sees _ as a special identifier that should be declared.

Example

var x = 0
(x, _) = (1, 2)

Current Output

Error: undeclared identifier: '_'

Expected Output

To work the same way as

var (x, _) = (1, 2)

(This was fixed at some stage, e.g. bug #2421)

Possible Solution

Additional Information

Checked with Nim 1.4 and Nim 1.5 devel.

@a-mr a-mr changed the title make tuple destructuring always work with _ (underscore) tuple destructuring doesn't always work with _ (underscore) Aug 18, 2021
@rockcavera
Copy link
Contributor

rockcavera commented Aug 23, 2021

By Nim's definitions, a variable couldn't even start with "_". Look here.

So, for me, the bug occurs in this situation: var (x, _) = (1, 2), as it compiles without error.

@konsumlamm
Copy link
Contributor

It seems Nim sees _ as a special identifier that should be declared.

Not really, it's special, but assigning to it discards the value (so you can't really declare it), you can't use it in an expression or anything.

So, for me, the bug occurs in this situation: var (x, _) = (1, 2), as it compiles without error.

That doesn't make any sense. _ is a special name, it's used to discard a value, for example the following (intentionally) works:

let _ = 42
# the following doesn't compile, since _ isn't a normal variable
# echo _

Now arguably (x, _) = (1, 2) shouldn't work, because _ = 42 doesn't work either, although one could argue that both should be valid. Fwiw, in Rust, this is called "destructuring assignment", but is currently unstable (see the RFC).

@rockcavera
Copy link
Contributor

It seems Nim sees _ as a special identifier that should be declared.

Not really, it's special, but assigning to it discards the value (so you can't really declare it), you can't use it in an expression or anything.

So, for me, the bug occurs in this situation: var (x, _) = (1, 2), as it compiles without error.

That doesn't make any sense. _ is a special name, it's used to discard a value, for example the following (intentionally) works:

let _ = 42
# the following doesn't compile, since _ isn't a normal variable
# echo _

Now arguably (x, _) = (1, 2) shouldn't work, because _ = 42 doesn't work either, although one could argue that both should be valid. Fwiw, in Rust, this is called "destructuring assignment", but is currently unstable (see the RFC).

Sorry, I didn't know the "_" functionality described here.

@bung87
Copy link
Collaborator

bung87 commented Oct 27, 2022

var (x, _) = (1, 2) is called "tuple unpacking". It seems the issue fit a feature request like @konsumlamm said, it's "destructuring assignment", maybe change a better title.

@metagn
Copy link
Collaborator

metagn commented Oct 28, 2022

Won't make the PR since it's new behavior but the patch for this is just skipping underscores here:

Nim/compiler/lowerings.nim

Lines 141 to 142 in e68a6ea

for i in 0..<lhs.len:
result.add newAsgnStmt(lhs[i], newTupleAccessRaw(tempAsNode, i))

metagn added a commit to metagn/Nim that referenced this issue Aug 23, 2023
Araq added a commit that referenced this issue Aug 24, 2023
* round out tuple unpacking assignment, support underscores

fixes #18710

* fix test messages

* use discard instead of continue

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
narimiran pushed a commit that referenced this issue Apr 17, 2024
* round out tuple unpacking assignment, support underscores

fixes #18710

* fix test messages

* use discard instead of continue

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
(cherry picked from commit 53d43e9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants