nushell/crates/nu-plugin-engine/src
Ian Manske 4d3283e235
Change append operator to concatenation operator (#14344)
# Description

The "append" operator currently serves as both the append operator and
the concatenation operator. This dual role creates ambiguity when
operating on nested lists.

```nu
[1 2] ++ 3     # appends a value to a list [1 2 3]
[1 2] ++ [3 4] # concatenates two lists    [1 2 3 4]

[[1 2] [3 4]] ++ [5 6]
# does this give [[1 2] [3 4] [5 6]]
# or             [[1 2] [3 4] 5 6]  
```

Another problem is that `++=` can change the type of a variable:
```nu
mut str = 'hello '
$str ++= ['world']
($str | describe) == list<string>
```

Note that appending is only relevant for lists, but concatenation is
relevant for lists, strings, and binary values. Additionally, appending
can be expressed in terms of concatenation (see example below). So, this
PR changes the `++` operator to only perform concatenation.

# User-Facing Changes

Using the `++` operator with a list and a non-list value will now be a
compile time or runtime error.
```nu
mut list = []
$list ++= 1 # error
```
Instead, concatenate a list with one element:
```nu
$list ++= [1]
```
Or use `append`:
```nu
$list = $list | append 1
```

# After Submitting

Update book and docs.

---------

Co-authored-by: Douglas <32344964+NotTheDr01ds@users.noreply.github.com>
2024-11-24 10:59:54 -08:00
..
interface Change append operator to concatenation operator (#14344) 2024-11-24 10:59:54 -08:00
plugin_custom_value_with_source Replace raw usize IDs with new types (#13832) 2024-09-30 13:20:15 +02:00
util doc: fix broken doc links (#13644) 2024-08-23 21:17:44 +02:00
context.rs Make get_env_var return a reference to a Value (#13987) 2024-10-02 13:05:48 +02:00
declaration.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
gc.rs Setup global cargo lint configuration (#13691) 2024-08-28 23:37:17 +02:00
init.rs Error on non-zero exit statuses (#13515) 2024-09-07 06:44:26 +00:00
lib.rs Split the plugin crate (#12563) 2024-04-27 12:08:12 -05:00
persistent.rs Fix try not working with let, etc. (#13885) 2024-09-23 06:44:25 -05:00
process.rs doc: fix broken doc links (#13644) 2024-08-23 21:17:44 +02:00
source.rs doc: fix broken doc links (#13644) 2024-08-23 21:17:44 +02:00
test_util.rs Split the plugin crate (#12563) 2024-04-27 12:08:12 -05:00