nushell/crates/nu-command/tests/commands/mod.rs
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

94 lines
1.1 KiB
Rust

mod alias;
mod all;
mod any;
mod append;
mod cal;
mod cd;
mod compact;
mod cp;
mod date;
mod def;
mod default;
mod do_;
mod drop;
mod each;
mod echo;
mod empty;
mod enter;
mod error_make;
mod every;
mod export_def;
mod find;
mod first;
mod flatten;
mod format;
mod g;
mod get;
mod glob;
mod group_by;
mod hash_;
mod headers;
mod help;
mod histogram;
mod insert;
mod into_filesize;
mod into_int;
mod last;
mod length;
mod let_;
mod lines;
mod ls;
mod math;
mod merge;
mod mkdir;
mod move_;
mod mut_;
mod n;
mod network;
mod nu_check;
mod open;
mod p;
mod parse;
mod path;
mod platform;
mod prepend;
mod print;
#[cfg(feature = "database")]
mod query;
mod random;
mod range;
mod reduce;
mod reject;
mod rename;
mod reverse;
mod rm;
mod roll;
mod rotate;
mod run_external;
mod save;
mod select;
mod semicolon;
mod seq_char;
mod shells;
mod skip;
mod sort_by;
mod source_env;
mod split_by;
mod split_column;
mod split_row;
mod str_;
mod table;
mod take;
mod touch;
mod transpose;
mod uniq;
mod update;
mod upsert;
mod use_;
mod where_;
#[cfg(feature = "which-support")]
mod which;
mod with_env;
mod wrap;
mod zip;