nushell icon indicating copy to clipboard operation
nushell copied to clipboard

Record destructuring

Open AlecsFerra opened this issue 2 years ago • 0 comments

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

AlecsFerra avatar Jul 12 '22 07:07 AlecsFerra