nushell/crates/nu-protocol/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
..
ast Change append operator to concatenation operator (#14344) 2024-11-24 10:59:54 -08:00
config Add proper config defaults for hooks (#14341) 2024-11-14 20:27:26 -08:00
debugger Make SpanId and RegId also use new ID struct (#13963) 2024-10-01 13:23:27 +02:00
engine Avoid recomputing fuzzy match scores (#13700) 2024-11-22 06:29:00 -06:00
errors Deprecate split-by command (#14019) 2024-11-21 10:47:03 +01:00
ir Fix try printing when it is not the last pipeline element (#13992) 2024-10-12 14:37:10 +08:00
pipeline Rely on display_output hook for formatting values from evaluations (#14361) 2024-11-19 21:04:29 +08:00
plugin Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
process Fix non-zero exit code errors in middle of pipeline (#13899) 2024-10-02 06:04:18 -05:00
value Change append operator to concatenation operator (#14344) 2024-11-24 10:59:54 -08:00
alias.rs Change the usage misnomer to "description" (#13598) 2024-08-22 12:02:08 +02:00
did_you_mean.rs Improve case insensitivity consistency (#10884) 2023-11-08 23:58:54 +01:00
eval_base.rs Change append operator to concatenation operator (#14344) 2024-11-24 10:59:54 -08:00
eval_const.rs Reduce duplication in history path construction (#13475) 2024-10-11 07:51:50 -05:00
example.rs Overhaul the plugin cache file with a new msgpack+brotli format (#12579) 2024-04-21 07:36:26 -05:00
id.rs Make SpanId and RegId also use new ID struct (#13963) 2024-10-01 13:23:27 +02:00
lev_distance.rs Rework for new clippy lints (#12736) 2024-05-02 19:29:03 +02:00
lib.rs Add top-level crate documentation/READMEs (#12907) 2024-07-14 10:10:41 +02:00
module.rs use command: Don't create a variable with empty record if it doesn't define any constants (#14051) 2024-10-20 23:12:57 +02:00
parser_path.rs Replace raw usize IDs with new types (#13832) 2024-09-30 13:20:15 +02:00
signature.rs Fix panic on too few arguments for custom function (#10395) 2024-09-27 23:39:45 +08:00
span.rs fix --ide-ast when there are errors (#13737) 2024-09-06 07:17:40 +08:00
syntax_shape.rs Make parsing for unknown args in known externals like normal external calls (#13414) 2024-07-21 01:32:36 -07:00
ty.rs Refactor config updates (#13802) 2024-10-11 18:40:32 +02:00