Skip to content

Introduce a spread operator #10598

@101313

Description

@101313

Related problem

Spread operator, the process of de-structuring as they call it in javascript.

Essentially, an elegant way to unpack objects, or records in our case, arrays and other data types which may be eligible without the need of introducing another component in the pipeline.

Destructuring record members

let rec = {a: 1, b: 2} 
let updated_rec = {...$rec, c: 4 }

as an alternative to its counterpart

let rec = {a: 1, b: 2}
let updated_rec = ($rec | merge {c: 4})

Example using a list:

$env.config.keybindings = ($env.config.keybindings | append {
    name: delete_one_word_forward
    modifier: control
    keycode: char_d
    mode: emacs
    event: {edit: deleteword}
})

Could be written as:

$env.config.keybindings =  [
    ...$env.config.keybindings
    {
        name: delete_one_word_forward
        modifier: control
        keycode: char_d
        mode: emacs
        event: {edit: deleteword}
     }
]

It allows for the omission of the pipe operator, hence the wrapping parentheses are omitted as well.

Another example of spreading into a list:

[10..0] | reduce -f [] {|i,a| [...$a $"Ready in ($i)" }

Describe the solution you'd like

There is already a similar syntax built in to the language, so using the triple dot operator seems to make sense as well, but its likely up for debate.

I composed the draft rather quickly so I didn't put a lot of thinking into demonstrating how it could improve the syntax amongst many cases but I'm sure there are better examples than the ones cited above.

Describe alternatives you've considered

The de-facto append.

Additional context and details

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:syntaxChanges to the grammar or syntax beyond parser bugfixescategory:enhancementNew feature or requestdelightthis feature would delight userssemanticsPlaces where we should define/clarify nushell's semantics

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions