Skip to content

Mutable "pipeline assignment" operator #14870

Open
@NotTheDr01ds

Description

Related problem

I'm not sure if I have the naming right, but when dealing with config (or other mutable variables), the convenience of the += and ++= operators only goes so far. When dealing with, for instance, a record that needs a merge, you still have to resort to $variable = $variable | merge {}.

We currently have:

╭───┬──────────┬────────────────╮
│ # │ operator │      name      │
├───┼──────────┼────────────────┤
│ 0 │ +=       │ PlusAssign     │
│ 1 │ ++=      │ ConcatAssign   │
│ 2 │ -=       │ MinusAssign    │
│ 3 │ *=       │ MultiplyAssign │
│ 4 │ /=       │ DivideAssign   │
╰───┴──────────┴────────────────╯

Describe the solution you'd like

A PipeAssign operator, probably |= which would allow a RHS expression that would simply use the value of the LHS as pipeline input. For example:

mut foo = "foo"
$foo |= str upcase
$foo
# => FOO

This comes in handy with longer $env.config entries.

# Without operator:
$env.config.hooks.env_change = (
    $env.config.hooks.env_change
    | merge deep --strategy=append {
          PWD: [{|before,after| null }]
      }
)

# With operator
$env.config.hooks.env_change |= merge deep --strategy=append {
    PWD: [{|before,after| null }]
}

Describe alternatives you've considered

No response

Additional context and details

No response

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestneeds-triageAn issue that hasn't had any proper looksyntaxChanges to the grammar or syntax beyond parser bugfixes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions