nushell / nushell/nushell

Record destructuring

Open
#6,021 4 comments 25 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A:syntax category:enhancement
Dominant language
Rust
Stars
40.5k
Forks
2.3k
Avg merge
1d 19h
Merged PRs (30d)
85

Description

Related problem

Let's take ls as an example, suppose we want to perform some operations on a file name in the current working directory:

ls | each { |it| 
  do-some-stuff $it.name
  do-some-stuff $it.name
  do-some-stuff $it.name
  do-some-stuff $it.name
}

Taking all the it fields in the scope it's cognitive heavier and requires the user to make a lot of typing when using a field more than 2/3 time, so someone will be tempted to just declare a new variable inside a block

ls | each { |it|
  let name = $it.name
  do-some-stuff $name
  do-some-stuff $name
  do-some-stuff $name
  do-some-stuff $name
}
Describe the solution you'd like

Support object destructuring directly in the block parameter

ls | each { |{ name }| 
  do-some-stuff $name
  do-some-stuff $name
  do-some-stuff $name
  do-some-stuff $name
}

Another cool idea could be supporting the same syntax inside a let definition like:

let { a, b, c } = do-some-stuff "test"

And maybe support directly renaming properties

let { a: an_a_alias } = do-some-stuff "test"
echo $an_a_alias
Describe alternatives you've considered

No response

Additional context and details

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the block-parameter example in Nushell and compare it with the proposed let-destructuring and property-renaming forms. Define which forms are in scope and verify that destructured names work in the block or let body, with tests covering the accepted syntax and resulting values.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, shell
Domain
cli
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.