nushell/crates/nu-cli/src
JT 13515c5eb0
Limited mutable variables (#7089)
This adds support for (limited) mutable variables. Mutable variables are created with mut much the same way immutable variables are made with let.

Mutable variables allow mutation via the assignment operator (=).

❯ mut x = 100
❯ $x = 200
❯ print $x
200

Mutable variables are limited in that they're only tended to be used in the local code block. Trying to capture a local variable will result in an error:

❯ mut x = 123; {|| $x }
Error: nu::parser::expected_keyword (link)

  × Capture of mutable variable.

The intent of this limitation is to reduce some of the issues with mutable variables in general: namely they make code that's harder to reason about. By reducing the scope that a mutable variable can be used it, we can help create local reasoning about them.

Mutation can occur with fields as well, as in this case:

❯ mut y = {abc: 123}
❯ $y.abc = 456
❯ $y

On a historical note: mutable variables are something that we resisted for quite a long time, leaning as much as we could on the functional style of pipelines and dataflow. That said, we've watched folks struggle to work with reduce as an approximation for patterns that would be trivial to express with local mutation. With that in mind, we're leaning towards the happy path.
2022-11-11 19:51:08 +13:00
..
completions Run a round of clippy --fix to fix a ton of lints (#7006) 2022-11-04 15:11:17 -05:00
menus Run a round of clippy --fix to fix a ton of lints (#7006) 2022-11-04 15:11:17 -05:00
commands.rs Remove perf flag to streamline logging configuration (#6834) 2022-10-21 10:20:21 -05:00
config_files.rs Run a round of clippy --fix to fix a ton of lints (#7006) 2022-11-04 15:11:17 -05:00
eval_file.rs Remove perf flag to streamline logging configuration (#6834) 2022-10-21 10:20:21 -05:00
lib.rs Split merging of parser delta and stack environment (#6005) 2022-07-14 17:09:27 +03:00
nu_highlight.rs feat: add search terms to category of strings (#5723) 2022-06-06 08:47:09 -05:00
print.rs add -e flag to print, to print the value to stderr (#5935) 2022-07-02 09:54:49 -05:00
prompt_update.rs Split blocks and closures (#7075) 2022-11-10 21:21:49 +13:00
prompt.rs Add support to render right prompt on last line of the prompt (#6781) 2022-10-23 16:18:26 +02:00
reedline_config.rs Split blocks and closures (#7075) 2022-11-10 21:21:49 +13:00
repl.rs Limited mutable variables (#7089) 2022-11-11 19:51:08 +13:00
syntax_highlight.rs Split blocks and closures (#7075) 2022-11-10 21:21:49 +13:00
util.rs New "display_output" hook. (#6915) 2022-11-06 13:46:40 +13:00
validation.rs Add an alias denylist for expansions (#4871) 2022-03-19 08:03:57 +13:00