nushell / nushell

A new type of shell
https://www.nushell.sh/
MIT License
31.72k stars 1.63k forks source link

`mut` with tables and indexes seems a little off? #7381

Open fdncred opened 1 year ago

fdncred commented 1 year ago

Describe the bug

how is mut supposed to work here? This seems off to me.

> mut t = [[a b]; [1 2]]
> $t.b = 5
╭───┬───┬───╮
│ # │ a │ b │
├───┼───┼───┤
│ 0 │ 1 │ 5 │
╰───┴───┴───╯

while this works to update column b index 0 to 5, it seems like it should be $t.b.0 = 5 but that gives

Error: nu::shell::assignment_requires_mutable_variable (link)

  × Assignment to an immutable variable.
   ╭─[entry #41:1:1]
 1 │ $t.b.0 = 5
   · ───┬──
   ·    ╰── needs to be a mutable variable
   ╰────

i get the same error with $t.0.b = 5

It gets a little worse when you table has more than one row.

> mut t = [[a b]; [1 2] [3 4]]
> $t
╭───┬───┬───╮
│ # │ a │ b │
├───┼───┼───┤
│ 0 │ 1 │ 2 │
│ 1 │ 3 │ 4 │
╰───┴───┴───╯
> $t.b = 5
> $t
╭───┬───┬───╮
│ # │ a │ b │
├───┼───┼───┤
│ 0 │ 1 │ 5 │
│ 1 │ 3 │ 5 │
╰───┴───┴───╯

I could acutally live with this one because it seems like the intent is to assign 5 to all of column b.

How to reproduce

see above

Expected behavior

$t.b.0 = 5 to work

Screenshots

No response

Configuration

key value
version 0.72.2
branch main
commit_hash 017a13fa3f465a6a62082fd3da1a373b39055cd8
build_os windows-x86_64
build_target x86_64-pc-windows-msvc
rust_version rustc 1.65.0 (897e37553 2022-11-02)
rust_channel 1.65.0-x86_64-pc-windows-msvc
cargo_version cargo 1.65.0 (4bc8f24d3 2022-10-20)
pkg_version 0.72.2
build_time 2022-12-05 14:24:44 -06:00
build_rust_channel release
features database, dataframe, default, trash, which, zip
installed_plugins custom-value generate, custom-value generate2, custom-value update, from parquet, gstat, inc, nu-example-1, nu-example-2, nu-example-3, periodic-table, pnet, query, query json, query web, query xml

Additional context

No response

kubouch commented 1 year ago

Could it be related to this PR? https://github.com/nushell/nushell/pull/7318

fdncred commented 1 year ago

Could it be related to this PR? https://github.com/nushell/nushell/pull/7318

maybe? @webbedspace what do you think?

sophiajt commented 1 year ago

btw, this is also true for upsert:

> [[a b]; [1 2] [3 4]] | upsert b 5
╭───┬───┬───╮
│ # │ a │ b │
├───┼───┼───┤
│ 0 │ 1 │ 5 │
│ 1 │ 3 │ 5 │
╰───┴───┴───╯
sophiajt commented 1 year ago

@fdncred - interesting: $t.0.b = 5 works but $t.b.0 = 5 doesn't

webbedspace commented 1 year ago

maybe? @webbedspace what do you think?

Both this and upsert just rely on Value.upsert_data_at_cell_path(), so it should be an easy fix to tweak that.

fdncred commented 1 year ago

Both this and upsert just rely on Value.upsert_data_at_cell_path(), so it should be an easy fix to tweak that.

Thanks for the feedback @webbedspace. Would you be willing to try and fix this behavior?

webbedspace commented 1 year ago

I'll do it… after #7309.

fdncred commented 12 months ago

On 0.85.1, I'm still getting this $t.0.b = 5 works but $t.b.0 = 5 doesn't